Thread: Incrementing Structure Member Array

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    36

    Incrementing Structure Member Array

    Will someone please tell me why this
    Code:
    *++p->vars = NULL;
    gives me this error
    error: lvalue required as increment operand
    vars is an array of pointers to char and p is a pointer to a structure. vars is a member of that structure.

    Thank you.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    To start off with, study you operator precedence table.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    Quote Originally Posted by hk_mp5kpdw View Post
    To start off with, study you operator precedence table.
    -> has the highest precedence so that should give me the address of my pointer array.

    ++ and * have the same precedence and associate from right to left so it should increment my array address then take the object in that address and assign NULL to it.

    That's my understanding of it and is exactly what I want to do. What am I missing?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What is vars declared as? If it's just a pointer, then what you are doing is illegal. If it's a double pointer, then you should be ok.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You say vars is an array of pointers to char. So:
    Code:
    char* vars[];
    We now that
    Code:
    *vars == vars[0]
    So what you have is:
    Code:
    (++(p->vars))[0] = NULL;
    So, yeah you are wrong, you need to assign a character

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    Quote Originally Posted by C_ntua View Post
    You say vars is an array of pointers to char. So:
    Code:
    char* vars[];
    We now that
    Code:
    *vars == vars[0]
    So what you have is:
    Code:
    (++(p->vars))[0] = NULL;
    So, yeah you are wrong, you need to assign a character
    Actually vars == &vars[0] so ++vars would be the same as &vars[1] and so *++vars == vars[1].

    Went back and reread section 5.3 of K&R and found my answer.
    The correspondence between indexing and pointer arithmetic is very close. By definition, the value of a variable or expression of type array is the address of element zero of the array. Thus after the assignment

    pa = &a[0];
    pa and a have identical values. Since the name of an array is a synonym for the location of the initial element, the assignment pa=&a[0] can also be written as

    pa = a;
    Rather more surprising, at first sight, is the fact that a reference to a[i] can also be written as *(a+i). In evaluating a[i], C converts it to *(a+i) immediately; the two forms are equivalent. Applying the operator & to both parts of this equivalence, it follows that &a[i] and a+i are also identical: a+i is the address of the i-th element beyond a. As the other side of this coin, if pa is a pointer, expressions might use it with a subscript; pa[i] is identical to *(pa+i). In short, an array-and-index expression is equivalent to one written as a pointer and offset.

    There is one difference between an array name and a pointer that must be kept in mind. A pointer is a variable, so pa=a and pa++ are legal. But an array name is not a variable; constructions like a=pa and a++ are illegal.
    Thanks for the replies everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. How to sort an array of pointers to structure
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 06-30-2008, 02:52 PM
  3. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM
  4. Multidimensional Array in a Structure
    By Night_Blade in forum C Programming
    Replies: 3
    Last Post: 04-04-2005, 08:14 PM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM