0

In a char array, you are required to have 2 of []. One of them is for storing the array index number and the other one is for storing the value for that index:

char u[1][50];

strcpy(u[0],"test");
strcpy(u[1],"test 2");

printf("%s \n",u[1]);

But, in an int array you just need one [].

Is it for the array index number or is it for the value?

Regardless of the answer to that question … Why isn't there 2 of []?

int z[100];

z[0] = 1;
z[1] = 2;

printf("%d \n",z[1]);

Why must there be "full accountability" for the char array but, for the int array, there does not need to be "full accountability"?

Why are there 2 standards?

Maybe you do not need "full accountability" for arrays?
Maybe the array's index number is considered default, organic and free stuff?

Well then in such case, for the char array, why are two of [] required?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • it's not required, but a `char[]` could be considered a `string`, and consequentially, a `char[][]` is `string[]` aka an array of strings. – Shark Sep 25 '19 at 10:30
  • @Shark *a `char[][]` is `string[]` aka an array of strings* No, just no. Lose the CS50 `string` abomination. Permanently. Hiding a pointer behind a `typedef` is misleading - at best. – Andrew Henle Sep 25 '19 at 10:59
  • See this answer: https://stackoverflow.com/a/57765755/12062404 This question gets asked a lot. A simple web search would give you a number of links to C tutorials that would answer your question, see this for example: https://overiq.com/c-programming-101/array-of-strings-in-c/ – gstukelj Sep 25 '19 at 11:13
  • @AndrewHenle i agree, and never mentioned any `typedef`s in there, was just trying to explain that a string is a `char[]`, and that a `char[][]` is an array of such "strings". and pointed out that a char array does not need two `[]`, but just one - but the purpose for which it is used, which is usually an array of strings - requires two. – Shark Sep 25 '19 at 12:14
  • "In a char array you are required to have 2 of []" --> No. `char u[1][50];` is not a `char` array but an [array 1 of array 50 of char](https://cdecl.org/?q=char+u%5B1%5D%5B50%5D%3B), a 2D array of `char`. – chux - Reinstate Monica Sep 25 '19 at 12:24

5 Answers5

9

You don't need two []:

int main() {
    char u[50];
    strcpy(u, "test");
    printf("%s \n", u);
}

It's just that strings are represented by a pointer to a character, so passing one char doesn't work. You tried that anyway, and then you changed the definition of the string to a multidimensional char array. This is redundant and causes issues when you do this:

char u[1][50];
strcpy(u[1],"test 2");

which is undefined behavior as the first array index has to be 0.

Perhaps your confusion comes from the comparison with int. If you want to print one char, use the %c format specifier (and then pass a char, not a char*). If you do that, it will be just like the int example:

int main() {
    char z[100];

    z[0] = '1';
    z[1] = '2';

    printf("%c \n", z[1]);
}
Blaze
  • 16,736
  • 2
  • 25
  • 44
2

Arrays in C follow this notation:

type array_name[num_elements];
type array_name[num_rows][num_elements];
...

So in your case you may want to have:

int z[100];

would be an array of 100 ints. To access any of them you use z[0] for the first int, z[1] for the second int, etc. till z[99].

Similarly you may want to have an array of chars like such:

char u[50];

to access the first character you use u[0], etc.

When it comes to multidimensional arrays, you may have:

char u[10][50];

which means you have 10 rows of 50 elements each. You can also call them strings, so 10 strings.

To access the first string you use u[0]. The last string is at u[9].

Now for each string you can access their elements like such: u[0][1] to access the second element of the first string.

strcpy requires as its first argument a string. So if you have u[10][50], you can pass u[9], which would be last string. You should not pass u[9][2] which would be the third char from the last string.

What you did is u[1][50], which means you so happen to have only 1 row in your list of strings. You still need to follow the same rules as for multidimensional arrays even though it is 1 string only. Normally you'd use u[50] and just pass u to strcpy

John
  • 1,012
  • 14
  • 22
1

Well then in such case..

for the char array why are two of [] required ?

It is not required...

Your confusion seems to come from the way string (aka text strings) are handled in C.

In C a string is a char array with the special requirement that a char in the array has the value NUL (aka '\0'). That special char represents end-of-string.

So in order to make an array of strings, you'll have to use "two []" - example:

char myStrings[10][101];

The variable myStrings can hold 10 strings and each of these can be up to 100 characters long (note: not 101 because you need one char for holding the special NUL character).

If you just do:

char someCharArray[101];

you can use it to store one string (max len 100) or use it for storing 101 individual chars.

Community
  • 1
  • 1
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
1

...Regardless of the answer to that question..
Why isn't there 2 of [] ?

Semantics may be affecting your understanding of arrays and their indexing requirements. C has arrays for int and char, each requiring a single set of [] to define. There is a special kind of char array in C commonly referred to as a C String, which is just a regular char array with the last character being the null terminator: \0. An array of C strings does commonly use two sets of brackets: [][]. The rules for representing a char array are the same as those for representing an int array. both can be described as a collection of values stored in a contiguous set of memory locations. To illustrate, example arrays of int, char and a C string follow:

int  numarr[]   = {99,104,97,114,32,97,114,114,97,121,0};
char chararr1[] = {'c','h','a','r',' ','a','r','r','a','y','\0'};
char chararr2[] = {99,104,97,114,32,97,114,114,97,121,0};
char string[]   = {"char array"}; 

The array values in each of the 4 examples above are identical. In most implementations, the int array is stored in a contiguous set 4 byte memory locations, while each of the char arrays are stored in a contiguous set of 1 byte memory locations. The three char arrays, if used in printf() would result in the same string output, as each is an array of char terminated with a null value.

printf("%s\n%s\n%s\n", chararr1, chararr2, string);

char array
char array
char array

More about the special char array - C string, and arrays of C string

char string[] = {"this is a string"}; 

A C string is defined an a null terminated array of char: The content de-marked between ^ is string defined above. Note the null termination.

|t|h|i|s| |i|s| |a| |s|t|r|i|n|g|\0|?|?|
^                                  ^

A contiguous set of C strings, or array of C strings however can be represented by using two set of square brackets:

char strings[2][20] = {"first string", "second string"};

This array of string would look like this in memory:

|f|i|r|s|t| |s|t|r|i|n|g|\0|s|e|c|o|n|d| |s|t|r|i|n|g|\0|?|?|?|
|       |           |      |
[0]     |           |      [1]   1st index used to access full string
        [0][4]='t'  [0][10]='n'  2nd index used to access array elements  

While each string can be accessed by using the first index,

eg. strings[0] = "first string" and strings[1] = "second string"

each character of each string can be accessed using the first and second index

eg. strings[0][0] == 'f'  and strings[1][0] == 's'
ryyker
  • 22,849
  • 3
  • 43
  • 87
0

You’re using u to store an array1 of strings, where each string can be up to 49 characters in length.

C doesn’t have an actual string type - in C, a string is a sequence of character values including a 0-valued terminator. Strings are stored as arrays of character type (char for ASCII, EBCDIC, or UTF-8 character encodings, wchar_t for "wide" encodings). So to store an array of strings, you need a 2D array of character type (or a 1D array of pointers to character type, where each element stores the address of another array, but we won’t get into that here).

So,

char name[51];

can store a single string up to 50 characters long (with an extra element for the 0 terminator):

printf( "Gimme a name: " );
scanf( "%s", name );

Compare that to

char names[10][51];

which can store up to 10 strings, each of which can be up to 50 characters in length:

for ( size_t i = 0; i < 10; i++ )
{
  printf( "Gimme a string: ");
  scanf( "%s", names[i] );
}


  1. Actually, your declaration for u only allows for a single array element - strcpy(u[1], “test 2”); is attempting to copy the string to the memory following u, which results in undefined behavior.

John Bode
  • 119,563
  • 19
  • 122
  • 198