hansdampf3
Programmer
Hi erveryone!
I'm quite new to C programming and I'm not sure whether the following little programm is correct and why it crashes with a single line uncommented. Heres the prog:
/***************************************/
#include <stdio.h>
int main(int argc, char *argv[])
{
const char *text[3][3] = {
{ "Hello", "I'm", "the"},
{"sample", "char", "array"},
{ "and", "I'm", "mean"}
};
/*text[0][0][0]='r';/* /* This line will crash. Why??? */
printf("Line 1: %c %c %c \n", text[0][0][0], text[0][0][1], text[0][0][2]);
/* correct output: H e l */
return 0;
}
/***************************************/
There are a few points I'm not quite sure of.
First of all, the definition of 'const char *[3][3]' ... is this really correct? The prog works as intended this way, but does the needed memory for the const "strings" gets allocated? Or is it as if I would do a
char *p;
sprintf (p, "testext"
which surely is very wrong.
The next point is the commented line 'text[0][0][0]='r';' which crashes the prog. Why? The following printf doesn't crash and outputs the right chars. This seems very strange to me.
Or maybe one could define the char array in a other way. From my understanding so far I would write:
char[][][] = {{...},{...},{...}};
but this doesn't compile. Error is "storage size of text unknown", but as I define the strings directly within the initialisation, the compiler should know the exact size. Or am I wrong?
Where's the difference in writing:
char[] = "This works!";
This compiles and works just fine. But if defining a char array, like in the prog given, it's somehow not possible. I don't understand the difference...
Would be very nice if someone could help me with these issues. Or anyone has a idea how to define a 3-dimensional char array. Surely I could use malloc for allocating the needed memory for the strings, but since they are const this shouldn't be neccessary.
Thanks and many greetz,
Sebastian
I'm quite new to C programming and I'm not sure whether the following little programm is correct and why it crashes with a single line uncommented. Heres the prog:
/***************************************/
#include <stdio.h>
int main(int argc, char *argv[])
{
const char *text[3][3] = {
{ "Hello", "I'm", "the"},
{"sample", "char", "array"},
{ "and", "I'm", "mean"}
};
/*text[0][0][0]='r';/* /* This line will crash. Why??? */
printf("Line 1: %c %c %c \n", text[0][0][0], text[0][0][1], text[0][0][2]);
/* correct output: H e l */
return 0;
}
/***************************************/
There are a few points I'm not quite sure of.
First of all, the definition of 'const char *[3][3]' ... is this really correct? The prog works as intended this way, but does the needed memory for the const "strings" gets allocated? Or is it as if I would do a
char *p;
sprintf (p, "testext"
which surely is very wrong.
The next point is the commented line 'text[0][0][0]='r';' which crashes the prog. Why? The following printf doesn't crash and outputs the right chars. This seems very strange to me.
Or maybe one could define the char array in a other way. From my understanding so far I would write:
char[][][] = {{...},{...},{...}};
but this doesn't compile. Error is "storage size of text unknown", but as I define the strings directly within the initialisation, the compiler should know the exact size. Or am I wrong?
Where's the difference in writing:
char[] = "This works!";
This compiles and works just fine. But if defining a char array, like in the prog given, it's somehow not possible. I don't understand the difference...
Would be very nice if someone could help me with these issues. Or anyone has a idea how to define a 3-dimensional char array. Surely I could use malloc for allocating the needed memory for the strings, but since they are const this shouldn't be neccessary.
Thanks and many greetz,
Sebastian