468,747 Members | 1,700 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,747 developers. It's quick & easy.

Can't access member - structures.

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[i])
}
}

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.
Mar 23 '06 #1
9 1903
gk245 wrote:
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[i])


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[i].x);

--
- Mark

Mar 23 '06 #2
gk245 wrote:
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[i])


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

--
Chris "x.f(y) == f(x, y) == (x, y).f" Dollin
The shortcuts are all full of people using them.
Mar 23 '06 #3
gk245 <to*****@mail.com> wrote in news:mn.ba657d631316c9c8.49793
@mail.com:
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[i])
}
}


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[i].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
--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

Mar 23 '06 #4

gk245 wrote:
I have something like this:
Missing:

#include <stdio.h>

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[i])
You surely mean:

printf("%d, %d, %f\n", next[i].x, next[i].y, next[i].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).

--
BR, Vladimir

Mar 23 '06 #5
gk245 <to*****@mail.com> wrote:
...
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[i])
}
}
...


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'

Mar 23 '06 #6
Roberto Waltman submitted this idea :
gk245 <to*****@mail.com> wrote:
...
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[i])
}
}
...


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[i] instead of next[i].x. But thats no excuse for not posting
real code. Leason learned, will do it next time.

Thanks.
Mar 23 '06 #7
gk245 wrote:
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[i])
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[i] 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[i].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.

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Mar 23 '06 #8
gk245 wrote:
I have something like this:

struct block
{
int x;
int y;
float z;
}
You are missing a semicolon above.
void somefunction (struct block next[])
{ Well, somefunction won't work.


Because
struct block { /* ... */ } void
is not a legal return type.
Mar 23 '06 #9
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[i].x) ;
}
}
main()
{
// arrary of initialized structure.

somefunction(initialized structure, int size)

}

Mar 24 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

73 posts views Thread by Sean Dolan | last post: by
1 post views Thread by Dmitri Vorobiev | last post: by
9 posts views Thread by tom arnall | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.