08-30-2022, 06:50 PM
(This post was last modified: 08-30-2022, 06:51 PM by Kernelpanic.)
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);
}