Pointers Question

This is a discussion on Pointers Question within the C Programming forums, part of the General Programming Boards category; Hi I was messing around with pointer in my program and ran into a problem. The compiler was giving me ...

  1. #1
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59

    Pointers Question

    Hi I was messing around with pointer in my program and ran into a problem. The compiler was giving me an initialization error for:
    Code:
    void swap(char *a[10], char *b[10])
    {
            char *temp[10] = *b;
    
            *b = *a;
            //*a = *temp;
            printf("\n\n\n--->%s\n\n\n\n",*b );
    }
    But when changed to:
    Code:
    void swap(char *a[10], char *b[10])
    {
            char *temp[10];
            *temp = *b;
    
            *b = *a;
            //*a = *temp;
            printf("\n\n\n--->%s\n\n\n\n",*b );
    }
    It works fine. Why can't I initialize this pointer on the same line I declare it? There must be a way.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If you don't know what the difference is between those two lines, you should pause and make sure you understand it.

    The first one attempts to set the array itself to contain the value of what b points to, or to be slightly pedantic, the first element of the array of b. The second example assigns the first element of temp to be that of the first element of the array b.

  3. #3
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    I understand, but how would I accomplish both the initialization and declaration in one line?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    What are you even trying to do? Are you trying to copy the contents of the array b into temp? If so, neither example actually is close to doing that.

    If you just want to set one element of temp to be equal to one element of b, I would recommend doing it on a separate line. I don't know of a way off hand to do it all on one line.

  5. #5
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    I want to accomplish what these statements do:
    Code:
            char *temp[10];
            *temp = *b;
    But on one line.

    As in: I want to initialize a char pointer and assign it the value of *b

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    So what are you trying to swap?

    Swap a and b themselves - seems kind of pointless, since the change won't affect the caller.

    Swap each pair of elements of a and b respectively. In which case the temp variable would just be
    char *temp = b[0]; // or *b if you're so inclined.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 02:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 11:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 02:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 12:04 PM

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