C
compilers do not charge by line used.
Declare one thing per line.
- it is easier to read
- it is easier to locate a name or type a value
- it is easier to change
- in case of an error you just get the precise line number from the compiler
compare
char (*array[10])[20],string[20] = "AEIOU"; array[2] = string
with
#include <stdio.h>
int main(void)
{
char string[20] = "AEIOU";
char(*array[10])[20];
array[2] = string;
}
line by line
char string[20] = "AEIOU";
Is is ok but you should always use const
as a reminder to yourself. If you do that an by mistake try to assign some value to string
compiler will warn you, not someone at the phone when your code crashes.
char array[2] = string;
Each array
is a pointer to a char[20]
so an &
is missing. Write:
char array[2] = &string;
not an initializer
array[2] = &string;
This is an assignment. Not an initializer.
This is an initilizer:
char(*array[10])[20] = {
NULL,
NULL,
NULL,
"A Test",
string,
NULL,
NULL,
NULL,
NULL,
NULL
};
This program
#include <stdio.h>
int main(void)
{
char string[20] = "AEIOU";
char(*array[10])[20] = {
NULL,
NULL,
NULL,
"A Test",
string,
NULL,
NULL,
NULL,
NULL,
NULL
};
array[2] = &string;
printf("\
#%d\t\"%s\"\n\
#%d\t\"%s\"\n\
#%d\t\"%s\"\n",
2, array[2],
3, array[3],
4, array[4]);
return 0;
}
prints
#2 "AEIOU"
#3 "A Test"
#4 "AEIOU"
Note that string
is const char[20]