hey friends i need help once again...

i need to know that how to accessing a structure in another function using pointers.i mean when we returning a pointer to the main function we have have the address of the structure.how to use that address to access the structure..pls guys help me.i'm still learning.:S

You use the pointer operator -> to access structure members. See line 18 in the code below for example.

struct person
{
   char lname[80];
   char fname[80];
};

struct person* foo()
{
   struct person* p = malloc(sizeof(struct person));

   <snip>
   return p;
}

int main()
{
   struct person* p = foo();
   printf("Last name is %s\n", p->lname);
}

thanx for your reply...but i'm little confuse at line 17.what's the use of foo();.Can you please explain that?..

foo() is nothing more than a generic function to illustrate a problem. You said in your original post that you wanted a function that returns a pointer to a structure, and that function foo() does exactly that.

If that is not what you mean, then you will have to post some code to show what you want.

oh! i got it..thank you friend.this is what i tried.(not the complete one).

#include<stdio.h>

struct matrix* read(void);

struct matrix{
	int mat_1[3][3],mat_2[3][3];
};

int main()
{
	int i,j;

	
	struct matrix* p = read();

	
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			
			printf("%d	",p->mat_1[i][j]);
		//	p++;
		}
		puts("");
	}
	
	
}


struct matrix* read()
{
	int i,j;
	struct matrix matA;
	
	for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
			matA.mat_1[i][j]=i+j;
			matA.mat_2[i][j]=i-j;
				
		}
	
	}
	
return &matA;
}

function read() can not return a pointer to an object that is declared locally on the stack. Why? Because stack objects are destroy immediately when the function returns.

There are two ways to fix the problem
(1) in read() declare matX as static, such as static struct matrix matX; The static keyword makes the object persistent, very much like a global object.

(2) declare matX as a pointer then call malloc() to allocate the memory for it. main() will have to free() the memory when done with it.

You can also declare the structure variable at the main function and pass its reference as argument to the read() function. Just update the read() function as:

void read(struct matrix*);

And use .(dot) operator at main function to print the values and use -> operator at the read() function to update the values.

I have written one program in which i am passing a pointer to a structure to function called display.

#include<stdio.h>
#include<conio.h>
struct data{
int ID;
char name[4];
};
typedef struct data record;
void display(record *[]);
record emp[2];

int i;
void main()
{
record *ptr;
ptr=&emp[0];
clrscr();
for(i=0;i<2;i++)
{
printf("enter name");
scanf("%s",&emp[i].name);
printf("enter ID");
scanf("%d",&emp[i].ID);
}
display(ptr);

}
void display(record *ptr[])
{

for(i=0;i<2;i++)
{
printf("%s %d",ptr[i]->name,ptr[i]->ID);
}
getch();
}

It does take the input data but does not display it.So please let me know what could be wrong in this display loop.

Line 14 you declare ptr as a record *, line 27 the function display takes as a parameter record *[] which is equivilent to record **. I am surprised that you did not get a compiler warning or error.

Correct the type that the function display takes as a parameter and then inside display use . instead of -> because ptr[i] now evaluates to an object not a pointer to an object.

Thanks Banfa for your reply.
I modified the code as you said.And now i get the first array displayed but not the second one.

#include<stdio.h>
#include<conio.h>
struct data{
int ID;
char name[4];
};
typedef struct data record;
void display(record *);
record emp[2];
int i,j;

void main()
{
record *ptr;
ptr=&emp[0];
clrscr();
for(i=0;i<2;i++)
{
printf("enter name");
scanf("%s",&emp[i].name);
printf("enter ID");
scanf("%d",&emp[i].ID);
display(ptr);

}
void display(record *ptr)
{    
for(i=0;i<2;i++)
{
printf("%d",ptr);
printf("%s %d",ptr[i].name,ptr[i].ID);
ptr++;
}
getch();
}

Please help!!

Thanks Banfa for your reply.
I modified the code as you said.And now i get the first array displayed but not the second one.

#include<stdio.h>
#include<conio.h>
struct data{
int ID;
char name[4];
};
typedef struct data record;
void display(record *);
record emp[2];
int i,j;

void main()
{
record *ptr;
ptr=&emp[0];
clrscr();
for(i=0;i<2;i++)
{
printf("enter name");
scanf("%s",&emp[i].name);
printf("enter ID");
scanf("%d",&emp[i].ID);
display(ptr);

}
void display(record *ptr)
{    
for(i=0;i<2;i++)
{
printf("%d",ptr);
printf("%s %d",ptr[i].name,ptr[i].ID);
ptr++;
}
getch();
}

Please help!!

In display you use i and an index to access pointer, however you also increment pointer so the second time round the loop you actually access the third item in the array no the second (because you moved along 1 twice).

Additionally at line 20 emp[i].name is already a pointer to the array you do not need the &.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.