Thread: Help understanding arrays and pointers

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    20

    Help understanding arrays and pointers

    I am learning C from a book, and I don't seem to understand fully the following paragraphs about arrays and pointers:


    Although many similarities exist between pointers and arrays, they have one key difference: An array isn't a variable.
    Pointers are variables. You can change their contents and change what lives at the memory addresses they point to. Arrays, on the other hand, are pretty much stuck in memory. You can't move them. You can't reassign them. You can change their variables, but that's about it.



    I don't seem to understand what is in BOLD (the things about arrays). Can somebody please explain and give easy examples to the text in BOLD. Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    Here is a simplistic explanation...

    Try and imagine a computer with only five blocks of memory.

    If, in a C program, you make the declaration..

    int MyInt;

    ...the first block is made available to hold the integer value you want to assign to MyInt. Anytime you adjust the value of MyInt, the value in block one will be changed. MyInt always exists in block one.

    The same applies if you were to declare...

    int ManyInts[4];

    The array ManyInts can hold four individual int values, and it will hold those values in blocks one to four. ManyInts will always relate to these four blocks.

    If you were to declare...

    int *pInt;

    ...block one would be assigned to the pointer-to-int, but the actual value it held (after assignment) would be another block's address (i.e. either block2, block3, block4 or block5]. You could then point the [pointer to any of the values in the other blocks of memory simply by changing the value in block1. The value of the integer in whichever block pInt points to can be found by de-referencing the pointer after it has been pointed.

    I think the key thing to remember is that a true variable holds an actual value, while a pointer holds the memory address of another variable.

    Code:
    #include <stdio.h>
    
    int
    main( int argc, char *argv[] )
    {
      int int1=1;
      int *pInt;
    
      pInt = &int1; /* The '&' provides the memory address */
                           /* of the int1 variable */
                           /* NOTE: pInt = int1 would be wrong */
    
      printf( "\n%d\n", *pInt );  /* The '*' de-references the pointer */
    
      printf( "\n%d\n", pInt ); /* This will print the memory address */
                                            /* of int1 (you would not normally do */
                                            /* this, and %d might not be the right */
                                            /* thing to use if you did - only do it */
                                            /* when you are sure you know what */
                                            /* you're doing) */
    }
    Obviously memory management is not as simple as block1 block2 etc, but I hope I made the point.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    148

    Re: Help understanding arrays and pointers

    Originally posted by James00
    Arrays, on the other hand, are pretty much stuck in memory. You can't move them. You can't reassign them. You can change their variables, but that's about it.
    Code:
    int a[10];
    int b[10];
    a = b; //ERROR
    Arrays are no first class objects,you cannot assign them the simple way.
    But with pointers it works
    Code:
    int *a = malloc(sizeof(int) * 10);
    int *b = a;//OK;
    Now a and b point to the same address.

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. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Need Help understanding arrays and pointers
    By flicka in forum C++ Programming
    Replies: 12
    Last Post: 10-04-2005, 09:45 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM