I wrote up this c program to get a feel for structure and pointers.
The problem
The program works fine atleast when it comes to taking in the input(confirmed by testing it via gdb) , what isn't right is that when it comes to printing the output data, it seems to be overwritten by something...
The Question
If the compiler reserves memory for a certain array , is it right to assume that after the "death" of a function , the reservation ends, and the memory is given to some other process,? and hence that wiould explain why the struct data is being over written?
The code
#include<stdio.h>
#include<stdlib.h>
typedef struct {int marks;int roll;} ADDRESS ;
void printer(ADDRESS* pointer,int len)
{ printf("-- Entering printing mode -- \n");
ADDRESS* p1;
p1=pointer;
int counter=0;
while ( counter < len)
{
printf(" --> Entry %d \n",counter);
printf("Marks : %d \n",p1->marks);
printf("Roll : %d \n ",p1->roll);
counter++;p1++;
}
}
ADDRESS *stater(int number)
{
printf("\t STANDBY FOR INPUT PART \t \n\n\n");
ADDRESS x[number];
ADDRESS *c,*i;
c=x;
i=c;
int counterf=number;
int counter=0;
while (counter!=counterf)
{
printf("\t Input the mark : ");
scanf("%d",&(c->marks));
printf("\t Input the roll : ");
scanf("%d",&(c->roll));
printf("\n");
counter++;
c=c+1;
}
return i;
}
int main()
{
ADDRESS *o=stater(4);
printer(o,4);
return 0;
}