3

foo is a struct with 5 scalar variables (A, B, C, D, F) and one array (E). What is confusing me is what f[0], f[1], and f[2] are in this context and what is happening here.

int     
bar(struct foo *f)
{
    f[1].C = f[0].B > f[2].C;
    f[0].E[-1] = f[0].D;
    f[0].A = f[1].C;
}

Are f[0], f[1], and f[2] individual structures with member variables? Can someone please explain? Thanks.

12
  • 1
    Segmentation fault? :) Commented Jul 10, 2014 at 3:37
  • Yes. f is a pointer to the first element of an array of struct foo. When addressed with an array subscript (like [1], for example), the compiler computes the address of the element and dereferences it (*(f + 1) in the example). Commented Jul 10, 2014 at 3:37
  • @someuser, not necessarily. Commented Jul 10, 2014 at 3:38
  • 1
    I never tried, in more than 15 years programming in C, to dereference a negative element in an array. Weird. Commented Jul 10, 2014 at 3:39
  • 1
    Haha I just noticed that negative index rslemos. That funny thing is I'm pretty sure the compiler would be okay with it. I've never tried it either, now I have this urge to go give it a shot for kicks. Commented Jul 10, 2014 at 3:40

4 Answers 4

2

Yes, in this context, f[0], f[1], etc., are elements of an array of type struct foo.

The more interesting thing to me is this line:

f[0].E[-1] = f[0].D;

I didn't realize negative indexes were allowed, but this question explains that array indexing is just pointer math, so it's an obfuscated way of saying:

f[0].D = f[0].D;

Which is basically useless as far as I know.

Also interesting:

f[0].C = f[0].B > f[2].C;

This would set f[0].C to a boolean, which is not usually compared with a > operator, so it's possible that different C members are used for different functions.

I feel that your confusion is warranted, given the strange nature of this function.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks (and to everyone else that answered). The function is part of a chapter review question about assembly notation and I actually don't think it is necessary to fully understand the function to answer the question correctly, but I wanted to make sure I understood what was happening here. Appreciate the answers everyone!
You're welcome, and welcome to SO! One of the best first posts I've seen.
E[-1] is not permitted if E is an array, because this attempts to access outside the bounds of E, causing undefined behaviour. (Recent versions of gcc -O3 might kill the entire function upon seeing this).
Just tried it with gcc 4.8.1 on Ubuntu 13.10 using -O3, and it worked.
I think the key is that the array is part of a struct, and not the first part, so there's a known memory allocation behind it. I don't know. I've never read standards except when they're posted on SO.
2

Are f[0], f[1], and f[2] individual structures with member variables?

Yes. f is a pointer to an array of struct foo instances f[0] is the first such member of that array, f[1] is the second member, etc. You might call it like this:

struct foo fArray[3];
// ... Initialize fArray[0], fArray[1], fArray[2] etc. ...
bar(fArray);

Comments

2

What you are doing is, you are passing a reference (pointer) to an array of struct foo to the function bar.

You must somewhere have a code that is similar to following:

struct foo  myFoos[10]; // an array with 10 elements of struct foo
struct foo *mallocedFoos;
// here goes some code to initialize the elements of the array
bar(&myFoos[0]);          // pass a reference to (address of/pointer to) the array

// or something like this is happening
mallocedFoos = malloc(sizeof(struct foo) * 10);
// here goes some code to initialize allocated memory
bar(mallocedFoos);        // pass the 'struct foo *' to the function

To understand the concept better, see this example.

Comments

1

In this case f is an array of structures Similar to
struct node = { int A; //A to D and F are scalar variables int B; int C; int D; int E[10]; //E is an array of integers int F; } struct node f[10]; //f is an array of structs

For more details you can also refer How do you make an array of structs in C?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.