简体   繁体   中英

Declaring 2D arrays for strings vs integers in C?

Why is it that when I declare a 2D array for strings I need to use '*'

char *month[12][10] = {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November",
"December"};

but when I declare it for integers, there's no need?

int numbers[2][3]={1,2,3,4,5,6};

Cause in C „strings” are represented or dealt with by pointers to its first character instead .

Or telling it the other way — there's no string type in C language, only pointers to characters are there. By convention lot of libraries consider pointer to character as begining of a „string”, terminated with 0x00 byte or accept „string” length as a parameter, but there's no string type.

when I declare it for integers, there's no need?

Cause integers are represented by themselves, not by theirs first byte address. When you use "something in quotes notation" it effectively gives your pointer to its first character and a pointer to character is noted as char *ptr in C.

When you see:

char *str = "some string";

it means variable str gets address of character s . And variable str is declared to point to a character , so it's char * . It's only a convention of some of libraries that using first letter of character you deal with string it begins with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM