How does this not work?!

This is a discussion on How does this not work?! within the C Programming forums, part of the General Programming Boards category; I am under the impression that one can declare a pointer, list, and assign to it values as if it ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    How does this not work?!

    I am under the impression that one can declare a pointer, list, and assign to it values as if it were an array. I attempted to do so, and got the error: lvalue required as left operand of assignment.

    Code:
    int main()
    {
       int* list;
       int a=2, b=3;
       list = &a;
       (list + 1)= &b; /* why does this not work? */
       return 0;
    }

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by cjavier View Post
    I am under the impression that one can declare a pointer, list, and assign to it values as if it were an array.
    That is true but:
    a) list needs to point to an array or chunk of memory first
    b) even if it were, you're adding the addresses of variables into that array
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    The closest thing I can make to your example would be:
    Code:
    int *list[2];
    int a = 2, b = 3;
    
    *list = &a;
    *(list + 1) = &b;
    Notice that I had to declare an array of 2 elements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Will this work?
    By Bladactania in forum C Programming
    Replies: 4
    Last Post: 02-11-2009, 11:16 AM
  2. How many of you actually work with C++?
    By CompiledMonkey in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2003, 09:55 PM
  3. Should this work?
    By Lynux-Penguin in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2003, 12:55 AM
  4. How much do You really work?
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 10-14-2002, 02:56 AM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21