Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6759735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:59:07+00:00 2026-05-26T13:59:07+00:00

I was working with a program that uses a function to set a new

  • 0

I was working with a program that uses a function to set a new value in the registry, I used a const char * to get the value. However, the size of the value is only four bytes. I’ve tried to use std::string as a parameter instead, it didn’t work.
I have a small example to show you what I’m talking about, and rather than solving my problem with the function I’d like to know the reason it does this.

#include <iostream>


void test(const char * input)
{
    std::cout << input;
    std::cout << "\n" << sizeof("THIS IS A TEST") << "\n" << sizeof(input) << "\n";
    /* The code above prints out the  size of an explicit string (THIS IS A TEST), which is 15. */
    /* It then prints out the size of input, which is 4.*/

    int sum = 0;
    for(int i = 0; i < 15; i++) //Printed out each character, added the size of each to sum and printed it out.
    //The result was 15.
    {
        sum += sizeof(input[i]);
        std::cout << input[i];
    }
    std::cout << "\n" << sum;
}
int main(int argc, char * argv[])
{
    test("THIS IS A TEST");
    std::cin.get();
    return 0;
}

Output:
THIS IS A TEST
15
4
THIS IS A TEST
15

What’s the correct way to get string parameters? Do I have to loop through the whole array of characters and print each to a string (the value in the registry was only the first four bytes of the char)? Or can I use std::string as a parameter instead?
I wasn’t sure if this was SO material, but I decided to post here as I consider this to be one of my best sources for programming related information.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:59:08+00:00Added an answer on May 26, 2026 at 1:59 pm

    sizeof(input) is the size of a const char* What you want is strlen(input) + 1

    sizeof("THIS IS A TEST") is size of a const char[]. sizeof gives the size of the array when passed an array type which is why it is 15 .

    For std::string use length()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a program that uses PHP's internal array pointers to iterate along
I am working on a program that uses IE to display its help pages.
Greetings, Forum. I'm working on a program in Python that uses Twisted to manage
I am working on a C program that uses a Union. The union definition
I am working on a program that uses Spring and obtains Hibernate transactions transparently
I am working on a program that uses mpi (openmpi 1.4.3) and pthreads, working
I'm working with a program that uses two-dimensional arrays of Strings (probably not that
I'm working on a multithreaded multi-platform Qt-based program that uses libssh2. The program was
I am working on a program that needs to create a multiple temporary folders
I'm working on a program that processes many requests, none of them reaching more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.