Can't access member - structures.

G

gk245

I have something like this:

struct block
{
int x;
int y;
float z;
}


void somefunction (struct block next[])
{
int i;
for (i =0, i < 4, i++)
{
printf("%i", next.x)
}
}

main()
{
// arrary of initialized structure.

somefunction(initialized structure)
}

Well, somefunction won't work. It just keeps saying that it can't
access the members x, y or z (depending on which one is being printed
out). I can't figure out why. I mean, it seems like i did it right,
since i delared a structure first, then made a variable of that type
and passed it to the function. Then i initialized the structure in
main, and passed that to somefunction.

Thanks.
 
M

Mark Odell

gk245 said:
I have something like this:

struct block
{
int x;
int y;
float z;
}


void somefunction (struct block next[])
{
int i;
for (i =0, i < 4, i++)
{
printf("%i", next.x)


x is *not* an array, it's an int. Why do expect this to work? However,
next[] is an array so you could try (%i looks so dated, try %d):

printf("%d", next.x);
 
C

Chris Dollin

gk245 said:
I have something like this:

Don't tells us "something like" what you wrote. Tell us
/exactly/ what you wrote. If you don't understand what's
wrong, how can you know what's relevant?
struct block
{
int x;
int y;
float z;
}


void somefunction (struct block next[])
{
int i;
for (i =0, i < 4, i++)

Didn't the compiler have a hissy fit at this point? The syntax
of a for loop uses `;`, not `,`, to separate the components.
{
printf("%i", next.x)


`next` is an array (well, a pointer actually), not
a struct of any kind. You surely meant `next.x`.
 
A

A. Sinan Unur

I have something like this:

Please post real code.
struct block
{
int x;
int y;
float z;
}


void somefunction (struct block next[])
{
int i;
for (i =0, i < 4, i++)
{
printf("%i", next.x)
}
}


Upon entering somefunction, next is an array of struct blocks.

#include <stdio.h>

struct block {
int x;
int y;
float z;
};

void block_printer(struct block next[], size_t nelem) {
size_t i;
for (i = 0; i < nelem; ++i) {
printf("%d\n", next.x);
}
return;
}

#define NELEM 3

int main(void) {
struct block next[NELEM] = {
{ 1, 2, 3, },
{ 4, 5, 6, },
{ 7, 8, 9, },
};

block_printer(next, NELEM);
return 0;
}

Sinan
 
V

Vladimir S. Oka

gk245 said:
I have something like this:

Missing:

#include said:
struct block
{
int x;
int y;
float z;

It's better:

double z;

unless you have explicit requirement for `float`.
}


void somefunction (struct block next[])
{
int i;
for (i =0, i < 4, i++)
{
printf("%i", next.x)


You surely mean:

printf("%d, %d, %f\n", next.x, next.y, next.z);
}
}

main()

Spell it out:

int main(void)
{
// arrary of initialized structure.

Why don't you show that initialisation, then?

struct block foo[4];
somefunction(initialized structure)

Syntax error above:

somefunction(foo);

return 0;
}

Well, somefunction won't work. It just keeps saying that it can't
access the members x, y or z (depending on which one is being printed
out).

You probably mean "won't compile".
I can't figure out why.

Re-read your C manual, the section on `struct`s and arrays.
I mean, it seems like i did it right,
since i delared a structure first,

So far so good...
then made a variable of that type

Nowhere to be seen...
and passed it to the function.

You didn't show this either.
Then i initialized the structure in
main,

No, you didn't...
and passed that to somefunction.

Nope.

If you want serious help, post a minimal compilable example that
exhibits your problem. Otherwise, we can only guess, and may get it
wrong (crystal balls are in short supply).
 
R

Roberto Waltman

gk245 said:
...
struct block
{
int x;
int y;
float z;
}

void somefunction (struct block next[])
{
int i;
for (i =0, i < 4, i++)
{
printf("%i", next.x)
}
}
...


You are trying to access the i'th element in the array 'x', member of
the single structure 'next'. This does not exist

What you want is member 'x' of the i'th element in the array of
structures 'next', of type 'block'
 
G

gk245

Roberto Waltman submitted this idea :
gk245 said:
...
struct block
{
int x;
int y;
float z;
}

void somefunction (struct block next[])
{
int i;
for (i =0, i < 4, i++)
{
printf("%i", next.x)
}
}
...


You are trying to access the i'th element in the array 'x', member of
the single structure 'next'. This does not exist

What you want is member 'x' of the i'th element in the array of
structures 'next', of type 'block'


Sorry guys, i feel stupid now. I had a syntax error...i was doing
next.x instead of next.x. But thats no excuse for not posting
real code. Leason learned, will do it next time.

Thanks.
 
P

Pedro Graca

gk245 said:
struct block
{
int x;
int y;
float z;
}

Missing semicolon.
void somefunction (struct block next[])

`next` is an array of struct block with an unknown number of elements.
Each element can be accessed with `next[element]`.

So the float member of the first element of the array is
next[0].z
{
int i;
for (i =0, i < 4, i++)

Syntax error. You only supplied the initialization part of the for();
the other parts (condition and increment) need to be separated from
each other with a semicolon.
{
printf("%i", next.x)


Missing semicolon.
No prototype in scope for printf(); C99 will not compile this program.
You need to #include <stdio.h>



`next` is an array of struct block. It doesn't have members; its
elements do.

next.x would mean the i-th (+1) element of the member x of the struct
next. For this to be valid next should be a structure with a member x
(right now next is an array), x should be an array with at least i+1
elements (right now its a simple integer).

You probably want to

printf("%i", next.x);
}
}

main()

C99 (I could tell because of your // comment below) requires one of

int main(void) {/*...*/}
int main(int argc, char ** argv) {/*...*/}

unless your implementation specifically documents some other prototype.
{
// arrary of initialized structure.

Invalid comment for C89.
You might want to keep comments to C89 allowed format (/* ... */) so
that your code compiles with a larger range of compilers. The C89 format
is more appropriate to usenet articles because it avoids line breaking
problems.
somefunction(initialized structure)

Missing semicolon.
}

Well, somefunction won't work. It just keeps saying that it can't
access the members x, y or z (depending on which one is being printed
out). I can't figure out why. I mean, it seems like i did it right,
since i delared a structure first, then made a variable of that type
and passed it to the function.

No you didn't. You passed a variable of type /array of struct block/.
Then i initialized the structure in
main, and passed that to somefunction.

You initialized the array elements and passed the array to somefunction.
 
Z

zhousqy

struct block
{
int x;
int y;
float z;



} ;


void somefunction (struct block next[], int size)
{
int i;
for (i =0; i < size; i++)
{
printf("%d", next.x) ;
}


}


main()
{
// arrary of initialized structure.

somefunction(initialized structure, int size)



}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top