-1

Hi guys I was thinking if I can assign pointer value to variable? Basically I have pointer that points to integer value and i want to assign it to another variable. Ie:

coursep->node = 1 //coursep is pointer to structure and node is structure variable
int var;
var = coursep->node; //I want variable to be 1

Can I do this? and how? It seems not to work when I try to assign it like this.

EDIT: Basically I am trying to put from pointer value to arrays or structures.

lines = lineCount(courses); //counts how many lines does file have
struct courses course[lines]; // creates array of structures with amount of lines

    for (i = 0; i < lines; i++) {
        struct courses *coursep;
        coursep = course;
        fscanf(co, " %c %d ", &coursep->courseName, &coursep->numberOfNodes);
        course[i].courseName = coursep->courseName;
        course[i].numberOfNodes = coursep->numberOfNodes;
        for (j = 0; j < coursep->numberOfNodes; j++) {
            fscanf(co, " %d", &(coursep->nodes[j]));
            var = *coursep->nodes[j];
            printf("%d\t", var);
        }
        fscanf(co, "\n");
    }

Structure:

struct courses{
char courseName;
int numberOfNodes;
int nodes[];
};

I cant seem to put it straight into array, because it prints out random rubbish, if I use pointers it seems to show correct values (everything is read from file), but when I try to put it all into array it seems tho throw random rubbish again =/ any ideas?

Shepard
  • 1,111
  • 5
  • 16
  • 32

4 Answers4

2

When you declare an empty array member at the end of a structure, it is a Flexible array member.

According to ISO/IEC 9899:TC3, Section 6.7.2.1 Structure and union specifiers, Constraint 16

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.


In your case, I don't think it is necessary to use a flexiable array member. You can change the definition of int nodes[] to int* nodes, and allocate memory for nodes in the for loop.

Lei Mou
  • 2,562
  • 1
  • 21
  • 29
0

try this

int *var;
var = &coursep->node;
Jonathan de M.
  • 9,721
  • 8
  • 47
  • 72
0

You need to change the nodes member variable to be a pointer rather than an array, and also to allocate memory for that array before assigning to it:

struct courses{
  char courseName;
  int numberOfNodes;
  int *nodes;
};

...

course[i].nodes = malloc(sizeof(int) * course[i].numberOfNodes);

(or something like that)

Yaniv
  • 991
  • 6
  • 13
  • if I add malloc it keeps complaining about `invalid use of flexible array member` any ideas how to fix that? – Shepard Dec 14 '12 at 03:00
  • Hah, I learnt something new. Check out the accepted answer to [this question](http://stackoverflow.com/questions/4379672/invalid-use-of-flexible-array-flexible-struct-array-as-a-member-of-another-stru) to understand why declaring an open array in a struct gives you unexpected behaviour. I'm updating my answer to reflect what you need to do. – Yaniv Dec 14 '12 at 04:17
0

One problem with this code is that coursep always points at the first element of your course array:

struct courses course[lines]; 

for (i = 0; i < lines; i++) {
    struct courses *coursep;
    coursep = course;
    /* ... */
}

...which means that almost everything in that array will probably be garbage.

Nate Kohl
  • 35,264
  • 10
  • 43
  • 55