473,224 Members | 1,496 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,224 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 2157
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
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.