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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
jshapy8
  • 1,983
  • 7
  • 30
  • 60
  • Just curiosity, what is the significance of `const char* str[5];` rather than just `char* str[5];`? – Rafaf Tahsin Sep 04 '15 at 03: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"` ? – M.M Sep 04 '15 at 03:49
  • @RafafTahsin Significance here in this question or in general ? – ameyCU Sep 04 '15 at 03: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 – Rafaf Tahsin Sep 04 '15 at 04: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. – xtofl Sep 04 '15 at 04:06
  • In C, _string_ is not a type, nor a `char *`, nor a pointer. A _string_ is a character array up to and including a terminating null character `'\0'`. – chux - Reinstate Monica Sep 04 '15 at 04:18

3 Answers3

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.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
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  
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
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;
ameyCU
  • 16,489
  • 2
  • 26
  • 41