I have this code
int main()
{
char **ppi;
ppi = malloc(sizeof(char *)*1);
ppi[0] = malloc(sizeof(char)+1);
ppi[1] = malloc(sizeof(char)+1);
ppi[2] = malloc(sizeof(char)+1);
strcpy(*ppi,"1");
strcpy(*(ppi+1),"2");
strcpy(*(ppi+2),"3");
printf("%s %s %s\n", *ppi,*(ppi+1),*(ppi+2));
char *pp[1];
pp[0]=malloc(sizeof(char)+1);
pp[1]=malloc(sizeof(char)+1);
pp[2]=malloc(sizeof(char)+1);
strcpy(*pp,"1");
strcpy(*(pp+1),"2");
strcpy(*(pp+2),"3");
printf("%s %s %s\n",*(pp+0),*(pp+1),*(pp+2) );
/*char *(*tags)[2] = malloc (20 * sizeof *tags);
for(int i=0;i<20;i++)
{
*(*(tags+i)+0)=*(tags_v+i);
tags[i][1]=tags_t[i];
//printf("%s %s \n",*(*(tags+i)+0),*(*(tags+i)+1));
}*/
// printf("%s\n",(*tags_1)[0]);
return 0;
}
But I am getting error stack smashing detected
I believe this is wrong pp[2]=malloc(sizeof(char)+1);
or any access after this line at index 2 for pp
array of pointers. But I can easily increase number of elements for pointer to pointer. Does this mean even array of pointers are kind of pointers but they are also array so increase number of elements can cause problems like stack smashing etc.
And can I get out of this error with array of pointers some how without not using array of pointers? I believe its stack error, probably because indexes are stack based for array of pointers. what if I create my array of pointers as global?
this question is related do I need to allocate space for pointer as well as space for memory area whose address will be kept in pointer in pointer to pointer and realloc