I am incrementing pointer variable for allocation but its giving me segfault message. Nothing goes wrong with the program since my program get executed until the end except return statement. So basically I am thinking
when we increment pointer like pointer+1
we are moving forward on contiguous memory and inside function -- may be in reality -- addresses of variables and function calls could be located in dispersed manner -- correct me if I am wrong. And when we increment pointer to move into next contiguous memory address and that memory could be anything (some other function/program) and my allocation is done on some area that serves memory for something else and may be something outside the memory region of abc
function. this is my code
#include <stdio.h>
struct abc{ char *c;};
void abc(struct abc **t1);
int main() {
struct abc *k;
abc(&k);
printf("%s\n",k->c);
// printf("%s\n",k->c);
printf("%s\n",(k+1)->c);
// printf("%s\n",(k+2)->c);
return 1;
}
void abc(struct abc **t1){
*t1=(struct abc *)malloc(sizeof(struct abc ));
(*t1)->c="hello";
*(t1+1)=(struct abc *) malloc((sizeof(struct abc )));
(*t1+1)->c="cool";
*(t1+2)=(struct abc *) malloc((sizeof (struct abc )));
//(*t1+2)->c="Super cool";
}
also another question is why it seems like I am having segment fault at return of main
function. since main memory area is different from abc
memory area. I need some explanation on this and question on is this way of allocation is wrong. if its not wrong the how to correct to make it work without seg fault
Update
#include <stdio.h>
struct abc{ char *c;};
void abc(struct abc **t1);
void abc2(struct abc *t1);
int main() {
struct abc *k;
abc(&k);
//printf("%s\n",k->c);
// k->c="hi"; Seg Fault causes because ->c is immutable from assigning string literal
// printf("%s\n",k->c);
printf("%s\n",(k)->c);
printf("%s\n",(k+1)->c);
printf("%s\n",(k+2)->c);
// / printf("%s\n",(k+2)->c);
return 1;
}
void abc(struct abc **t1){
*t1=(struct abc *)malloc(sizeof(struct abc)*3);
abc2((*t1));
abc2((*t1+1));
abc2((*t1+2));
}
void abc2(struct abc *t1){
(t1)->c="cool";
}
the above code works without segFault but its different from the answers so what's wrong with the above code
As below also works
#include <stdio.h>
struct abc{ char *c;};
void abc(struct abc **t1);
void abc2(struct abc *t1);
int main() {
struct abc *k;
abc(&k);
//printf("%s\n",k->c);
// k->c="hi"; Seg Fault causes because ->c is immutable from assigning string literal
// printf("%s\n",k->c);
printf("%s\n",(k)->c);
printf("%s\n",(k+1)->c);
printf("%s\n",(k+2)->c);
// / printf("%s\n",(k+2)->c);
return 1;
}
void abc(struct abc **t1){
*t1=(struct abc *)malloc(sizeof(struct abc)*3);
(*t1)->c="Cool";
(*t1+1)->c="more cool";
(*t1+2)->c="super cool";
//below also works
abc2((*t1));
abc2((*t1+1));
abc2((*t1+2));
/* (*t1)->c="hello";
//*(t1+1)=(struct abc *) malloc((sizeof(struct abc)));
(*t1+1)->c="cool";
//*(t1+2)=(struct abc *) malloc((sizeof (struct abc)));
(*t1+2)->c="Super cool";*/
}
void abc2(struct abc *t1){
(t1)->c="cool";
}