473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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 2164
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances'...
6
by: blueblueblue2005 | last post by:
here is a friend function of Class Array, which has two private data member: int size, int *ptr // Array's public member function to return size int getSize() const { return size; } friend...
7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
1
by: Dmitri Vorobiev | last post by:
Hello group, Is it possible to check the presence of a structure member at compile time using only the C preprocessor? What I have in mind could look like the following. Suppose the...
9
by: tom arnall | last post by:
does anyone know of a utility to do a recursive dump of object data members? -- Posted via a free Usenet account from http://www.teranews.com
9
by: cman | last post by:
What are the mechanics involved in the calculation of an offset of a structure member, demonstrated in this piece of code? #define list_entry(ptr, type, member) \ ((type *)((char...
7
by: CaptainBly | last post by:
Okay I've been spending lots of time on this and it's giving me a migraine so I go humble and ask the guru's here. I have several structs typedef struct vert { int x,y,z; struct vert *...
3
by: dolphin | last post by:
Hello everyone! Can a static member function access non-static member? I think it is illegal.Is it right?
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.