3

Although I am using C++, as a requirement I need to use const char* arrays instead of string or char arrays. Since I am new, I am required to learn about how to use const char*. I have declared my const char* array as const char* str[5]. At a later point in the program I need to populate each on of the 5 elements with values.

However, if I try to assign a value like this:

const char* str[5];
char value[5];
value[0] = "hello";
str[0] = value[0];

It will not compile. What is the proper way to add a char array to char to a const char* array and then print the array? Any help would be appreciated. Thanks.

6
  • Just curiosity, what is the significance of const char* str[5]; rather than just char* str[5];? Commented Sep 4, 2015 at 3:45
  • You seem mixed up. char value[5] is an array of 5 characters. value[0] is a single character out of those 5. How do you expect a single character to hold the value "hello" ? Commented Sep 4, 2015 at 3:49
  • @RafafTahsin Significance here in this question or in general ? Commented Sep 4, 2015 at 3:53
  • mostly here in this question. what is the significance of using const char* str[5]; here? and for general purpose, so far I know, the significance of const char* str[5]; is that it's a variable pointer which points to a const variable. Does it have any other significances? >>> @ameyCU Commented Sep 4, 2015 at 4:02
  • It's not bad to learn naked pointer logic, but it is a good thing to define your concepts: typedef const char* MyString; MyString strings[5]; This will for sure help avoiding mistakes. Commented Sep 4, 2015 at 4:06

3 Answers 3

8
  1. The string "hello" is made up of 6 characters, not 5.

    {'h', 'e', 'l', 'l', 'o', '\0'}
    
  2. If you assign the string when you declare value, your code can look similar to what your currently have:

    char value[6] = "hello";
    
  3. If you want to do it in two separate lines, you should use strncpy().

    char value[6];
    strncpy(value, "hello", sizeof(value));
    
  4. To place a pointer to value in the list of strings named str:

    const char * str[5];
    char value[6] = "hello";
    str[0] = value;
    

    Note that this leaves str[1] through str[4] with unspecified values.

Sign up to request clarification or add additional context in comments.

4 Comments

How about char value[] = "hello";?
@CaptainObvlious: Sure. But it's not obvious to revolution how many bytes that occupies.
@BillLynch, your last example str[0] = value; seems very dangerous. It assigns 4 byte pointer address value to 1 byte char value place.
str in this example is a list of strings.
1

Various methods to populate const char* str[5] later point in the program.

int main(void) {
  const char* str[5];

  str[0] = "He" "llo";  // He llo are string literals that concat into 1

  char Planet[] = "Earth";  
  str[1] = Planet;  // OK to assign a char * to const char *, but not visa-versa

  const char Greet[] = "How";
  str[2] = Greet;

  char buf[4];
  buf[0] = 'R'; // For a character array to qualify as a string, need a null character.  
  buf[1] = 0;   // '\0' same _value_ as 0  
  str[3] = buf; // array buf converts to address of first element: char *

  str[4] = NULL; // Do not want to print this.

  for (int i = 0; str[i]; i++)
    puts(str[i]);
  return 0;
}

.

Hello  
Earth  
How  
R  

Comments

0
value[0] = "hello";

How can this hold "hello". It can hold only one character.

Also value[5] is not enough for this . There will be no space for '\0' and program will exhibit UB.

Thus either use value[6] -

char value[6];
strncpy(value,"hello",sizeof value);

Or declare like this -

char value[]="hello";

And after that just make the pointer point to this array. Something like this-

str[0]=value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.