Thread: Beginner's question concerning pointers and arrays

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    2

    Beginner's question concerning pointers and arrays

    I am having trouble understanding why I can't get my array of strings to print using a pointer.

    Here is my code so far:

    Code:
    #include <stdio.h>
    
    int main() {
    
      int a[] = {1, 10, 100, 1000};
      int *b = a;
      char *c[] = {"hello", "how", "are", "you?"};
    
      printf("%d\n", *b);
      printf("%d\n", *++b);
      printf("%d\n", *++b);
    
      printf("%s\n", *c);
      printf("%s\n", *++c);
    
      return 0;
    }
    When I run try to compile this code, I receive this error:

    Code:
    pointer-test.c: In function ‘main’:
    pointer-test.c:14: error: lvalue required as increment operand
    Why does the same pointer syntax not work for an int array and a string array?

    Thanks for any help.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    c is an array of char pointers, which means c is actually an array, and that's why you can't use ++ operator with it. Whereas b is a poniter(not an array), that's why ++b is legal. Take this as an example.
    Code:
    int a[]={1,2,3,4};
    printf("%p",(void *)++a); // again the same error coz a is an array
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can do this:
    Code:
    printf("%s\n", ++(*c));
    but that ++ == sizeof(char), which is not what you want.

    Consider subscript notation:
    Code:
    printf("%s\n", c[1]);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    2
    Thanks for the help. I appreciate both of your replies.

    I have two further questions about the following code:

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
    
      int a[] = {1, 10, 100, 1000};
      int *b = a;
      char *c[] = {"hello", "how", "are", "you?"};
      char **d = c;
    
      printf("%d\n", *b);
      printf("%d\n", *++b);
      printf("%d\n", *++b);
      printf("%d\n", *++b);
    
      printf("%s\n", *d);
      printf("%s\n", *++d);
      printf("%s\n", *++d);
      printf("%s\n", *++d);
    
      while (--argc > 0)
        printf("%s\n", *++argv);
    
      return 0;
    }
    1) Why is it that I can manipulate argv, which is an array of char pointers, using ++ but couldn't do the same to c (in the original post)?

    2) Is my way of using char **d sloppy at all? After reading BEN10's answer, I had the idea of using a pointer to a pointer to a char, but I don't know if that is a convoluted way of doing things.

    My goal with this little program is to understand pointers better and not use indexing, since I already understand how indexing works.

    Thanks again for any help.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 1) Why is it that I can manipulate argv, which is an array of char pointers, using ++ but couldn't do the same to c (in the original post)?
    Because it's a pointer.
    char *argv[] could equally be written as char **argv

    Using [] in the context of a function parameter doesn't add any "true array" properties to it.
    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.

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by rcomj View Post
    Thanks for the help. I appreciate both of your replies.

    I have two further questions about the following code:

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
    
      int a[] = {1, 10, 100, 1000};
      int *b = a;
      char *c[] = {"hello", "how", "are", "you?"};
      char **d = c;
    
      printf("%d\n", *b);
      printf("%d\n", *++b);
      printf("%d\n", *++b);
      printf("%d\n", *++b);
    
      printf("%s\n", *d);
      printf("%s\n", *++d);
      printf("%s\n", *++d);
      printf("%s\n", *++d);
    
      while (--argc > 0)
        printf("%s\n", *++argv);
    
      return 0;
    }
    1) Why is it that I can manipulate argv, which is an array of char pointers, using ++ but couldn't do the same to c (in the original post)?

    2) Is my way of using char **d sloppy at all? After reading BEN10's answer, I had the idea of using a pointer to a pointer to a char, but I don't know if that is a convoluted way of doing things.

    My goal with this little program is to understand pointers better and not use indexing, since I already understand how indexing works.

    Thanks again for any help.
    1. When you use any array as an argument to function it decomposes into pointer and thus you can do ++ with argv. Here's an example.
    Code:
    #include<stdio.h>
    void f(int []);
    int main(void)
    {
    	int a[]={1,2,3};
    	printf("%p",(void *)a);
    	f(a);
    	getch();
    }
    void f(int b[])
    {
    	printf("\n%p",(void *)++b);//++ is legal here
    }
    2. It's fine I guess.
    Last edited by BEN10; 08-11-2009 at 09:14 AM.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 01:27 AM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM

Tags for this Thread