Hello,

i'm still trying to learn C, and I have a new question ;).

I want to declare a struct, and use a function to manipulate the entries. If I change the values within a function it's ok; however, when i access the members outside the function, they're not defined anymore.

How do i change the values in the struct such that a different function can use it as well? I thought using a pointer to struct would be 'good enough', but apparently it isn't ;).

Thanks in advance,

#include <stdio.h>
#include <stdlib.h> // for drand48
#include <math.h>

struct atom{
	double x,y,z;
	double vx,vy,vz;
	double fx,fy,fz;
	int id;
};

void initialpos(struct atom *name, int N);

const int N=100;

int main()
{
	struct atom *patom[100];
	
	int i;
	for (i = 0; i < N; i++)
	{
		patom[i]=(struct atom *)malloc(sizeof(struct atom));
	}
	
	for (i = 0; i < N; i++)
	{
		if (patom[i]==NULL)
		{
			printf("out of MEM\n");
			return 1;
		}
	}
	
	/* give atoms their initial position */
	initialpos(patom[0],N);
	
	for (i = 0; i < N; i++)
	{
		printf("pos. of %d: %f\t %f\t %f\t \n", i, patom[i]->x, patom[i]->y, patom[i]->z);
	}
	
	
	return 0;
}

void initialpos(struct atom *name, int N)
{	
	int i;
	
	for (i = 0; i < N; i++)
	{
		name[i].x=drand48()*20;
		name[i].y=drand48()*20;
		name[i].z=drand48()*20;
		printf("pos of %d: %f\t %f\t %f\t \n", i, name[i].x, name[i].y, name[i].z);
	}
}

Recommended Answers

All 6 Replies

void initialpos(struct atom *name, int N)
{    
    int i;
    
    for (i = 0; i < N; i++)
    {
        name[i].x=drand48()*20;
        name[i].y=drand48()*20;
        name[i].z=drand48()*20;
        printf("pos of %d: %f\t %f\t %f\t \n", i, name[i].x, name[i].y, name[i].z);
    }
}

Taking this as an example:
name.x=drand48()*20;
You are assigning to a field of a structure, but that is not a field of a structure. It is a pointer to a field of a structure. It can be done in two ways:
(*name).x = drand48() * 20; or
name->x = drand48() * 20;
Same thing is going on with the others and even with the parameters in the printf();

I tried changing the function like that as well; but it still doesn't work.
Maybe I'm silly, but I just don't see it.

(and thanks for helping)

void initialpos(struct atom *name, int N)
{    
    int i;
    
    for (i = 0; i < N; i++)
    {
        name[i]->x=drand48()*20;
        name[i]->y=drand48()*20;
        name[i]->z=drand48()*20;
        printf("pos of %d: %f\t %f\t %f\t \n", i, name[i].x, name[i].y, name[i].z);
    }
}

>but it still doesn't work.
What's not working?. What's supposed to happen and it isn't?.
You forgot to change the arguments also in the printf().

Now it's like this:

void initialpos(struct atom *name, int N)
{	
	int i;
	
	for (i = 0; i < N; i++)
	{
		name[i]->x=drand48()*20;
		name[i]->y=drand48()*20;
		name[i]->z=drand48()*20;
		printf("pos of %d: %f\t %f\t %f\t \n", i, name[i]->x, name[i]->y, name[i]->z);
	}
}

(i forgot the printf function the previous time).
Now it's complaining about 'invalid type argument of '->' '
I'll explain what i want to do.
In main() a the function initialpos() is called, to give a particle some initial position. But this position is needed later on as well, so i want to keep the values too when the function is finished.
When i did it like in the opening post, the values are written to the struct, but after the function is fnished, the elements are gone.

Hope you understand what I mean. Thanks for the help so far,

Needs some changes:
initialpos(&patom[0],N); instead of initialpos(patom[0],N); the & is needed if you want to pass it the address of a structure.
void initialpos(struct atom *name[], int N) instead of void initialpos(struct atom *name, int N) *name[] is the beginning of the array of structures.

Thanks! it's working now. It makes sense: before I only gave one element as an argument, but instead I had to give an array with starting address &patom[0]. Now all variables in the struct are kept (because the array in the argument).
I'd never think of that! (even though it makes sense now). Thanks a lot! Now I can finally go to bed.. it's 2 AM here ;).
(If something doesn't work, I keep wondering how to solve it... and then I can't sleep. )
Thanks again!

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.