I know that arrays are static elements used when you have a pre-determined size that can be used. But speaking of initialization, when the size is already set next to the array, I would like to know, essentially, why you can not use other types of arrays like C (pointer notation) strings. For example I can write:
#include <stdio.h>
int main(void)
{
char *sstring = "Olá, Mundo!";
char schars[] = {'O', 'l', 'a', 'char *sstring = "Olá, Mundo!";
char *schars = {'O', 'l', 'a', '#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int arrayInt[][3] = {{1, 2, 3}, {4, 5, 6}};
char *arrayChar[] = {"PALAVRA", "teste", "HEY"};
char **names = malloc(3 * sizeof(char *));
*names = "Teste";
*(names + 1) = "de";
*(names + 2) = "Arrays";
printf("%d\n", arrayInt[1][2]);
printf("%c\n", arrayChar[0][4]);
printf("Nome: %s %s %s\n", *names, *(names + 1), *(names + 2));
return 0;
}
'};
int *mnumbers = {1, 2, 3, 4, 5};
'};
int mnumbers[] = {1, 2, 3, 4, 5};
printf("Sstring : %s\n", sstring);
printf("Schars : %s\n", schars);
printf("Mnumber : %d\n", *mnumbers);
return 0;
}
But on the other hand, I can not write:
#include <stdio.h>
int main(void)
{
char *sstring = "Olá, Mundo!";
char schars[] = {'O', 'l', 'a', 'char *sstring = "Olá, Mundo!";
char *schars = {'O', 'l', 'a', '#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int arrayInt[][3] = {{1, 2, 3}, {4, 5, 6}};
char *arrayChar[] = {"PALAVRA", "teste", "HEY"};
char **names = malloc(3 * sizeof(char *));
*names = "Teste";
*(names + 1) = "de";
*(names + 2) = "Arrays";
printf("%d\n", arrayInt[1][2]);
printf("%c\n", arrayChar[0][4]);
printf("Nome: %s %s %s\n", *names, *(names + 1), *(names + 2));
return 0;
}
'};
int *mnumbers = {1, 2, 3, 4, 5};
'};
int mnumbers[] = {1, 2, 3, 4, 5};
printf("Sstring : %s\n", sstring);
printf("Schars : %s\n", schars);
printf("Mnumber : %d\n", *mnumbers);
return 0;
}
Even though the size is known, after all I'm initializing the arrays. Why does this happen? Why, even in an array of chars initialized with parentheses, is it not possible to treat them as pointers?
This occurs even with arrays and larger-sized (obviously) pointers:
%pre% It is not possible to do int **arrayInt = {{1, 2, 3}, {4, 5, 6}};
and it is still necessary to inform one dimension of the array, even by explicitly declaring it. A char *arrayChar[] = {"PALAVRA", "teste", "HEY"};
mixed form is still possible, but char *arrayChar[] = {{'O', 'l', 'a', '
is not. It seems that the use of %code% brackets is linked to initialization with %code% keys. I wanted to know the reason. []
'}, {'M', 'u', 'n', 'd', 'o', '{}
'}};