(08-30-2022, 06:50 PM)Kernelpanic Wrote:Quote:Oh yeah, you can create multidimensional arrays in C, but a string literal such as "Hello, world!" is treated as a one dimensional array of characters, with a NULL (0) as the last element.
If you try to create a multidimensional string array in C, you are really only creating an array of pointers to strings.
You have a fundamental problem understanding how multidimensional arrays are stored in the computer.
Regardless of whether it is one, two or three-dimensional, it is stored linearly in the computer. But the numerical example shows that the individual values can only be accessed via <row> - column>. The same applies to character strings.
Character strings in C are basically fields of the "char" type. And regarding of a pointer is an unindexed array name a pointer to the first array element:
char x[10];
The following two statements are identical:
x
&x[0]
Example of a one- and two-dimensional string field:
Code: (Select All)#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
//One-dimensional array
char namen[] = "Rio de Janeiro";
char *zgr = namen;
//Two-dimensional array
char namenfeld[3][9] = {"Berlin", "Tokio", "Katmandu"};
int feldnummer;
printf("\n");
while( *zgr )
{ printf("%c", *zgr++); }
printf("\n\n");
for(int i = 0; i < 3; i++)
{
printf("%s ", namenfeld[i]);
}
printf("\n");
printf("\nFeldnummer des Namesn der angezeigt werden soll (0 bis 2): ");
scanf("%d", &feldnummer);
printf("[%d] ist die Adresse von: %s", feldnummer, namenfeld[feldnummer]);
printf("\n\n");
return(0);
}
There IS a misunderstanding here, but don't be so self-assured when assigning blame. Maybe I didn't explain things thoroughly enough, or maybe there's a language barrier in the way.
I didn't respond to your previous post about multi-dimensional arrays because you were talking about declared arrays, not quoted strings. Not wanting to post a correction, and having no need to show off, I let your post slide unanswered.
I am describing apples, you are describing oranges. I was talking about strings, which in C are arrays of characters all lined up in neat little rows in memory. Notice that I wrote "rows": a row has only ONE dimension. Each quoted string is a single row of characters, all by itself. It may have many columns, but it only has one row. I was NOT talking about arrays of strings, which are a completely different things.
A "string" - with double quotes around it like "this" - is a simple character array, with ONE DIMENSION. I am not talking about an array of strings, which is an array of pointers to strings, with the strings themselves being stored elsewhere.
This is an easy point of confusion for people who don't regularly program in C or assembly, which I have been doing for, oh, about 30 years now in C, and 40+ years in assembly.
Yes, you can create multi-dimensional arrays of characters. You can even NULL-terminate each row to create a block of C-style strings accessible through pointers. You can even create a pointer to a multi-dimensional array and load the address of your quoted string literal into it, to access it through the pointer. Those are NOT what I have been talking about.
My subject was string literals. When a C compiler encounters a string literal like this: "this is a character string", the compiler stores that string with other program data and ensures that the string will be loaded into memory when the executable is run. The compiler substitutes a pointer - the address of the string - into to the program at the point where the string was used. That pointer is a character pointer (char *), and can be used as such. To get technical, source which reads letter="abcde"[3] will be translated by the compiler to letter=*(<address_of_abcde> + (3 * sizeof (char))).
C strings have only ONE row: a string in C is a one dimensional array of characters, real characters all side-by-side in memory, NOT an array pointers to strings. Just as each quoted string in C source code stands alone, so do each of those compiler-generated character arrays stand alone.
THAT is what I was talking about.
A string literal's address can be used just like the address a ONE DIMENSIONAL array. "string"[i] is perfectly valid C, while "string"[i][j] will generate a compile-time error because it makes no sense.
Quote:Regardless of whether it is one, two or three-dimensional, it is stored linearly in the computer. But the numerical example shows that the individual values can only be accessed via <row> - column>. The same applies to character strings.
As I've tried to explain, quoted character strings can only be accessed via <column>. (Unless you want to play with pointers, but that was not what I was talking about.)
If you want to experiment, arrays in C can be accessed however you want, if you understand how the array data is laid out and are willing to play with pointers. You can get experience using "#define"s in the preprocessor to make life a bit easier.