Thread: Loading an array using pointers

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    26

    Loading an array using pointers

    I am trying to use pointers to preload an array. I know how to do it as an array but the pointer method has my head twisted around.

    The Array is defined as Array[Size]

    If I used a function void load (int *loadmem) , how would I load the entire array with a single ( non-zero ) interger value. Would a FOR loop work from 0 to Size. But how do I tie the Array to a pointer and then increment the location in the pointer?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    /*fills an array with 0's*/
    char array[SIZE], *p;
    for(p = &array[0]; p < &array[SIZE - 1]; )
        *p++ = 0;
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    26
    Does this mean that if I wanted to load every element of the array with the number 8 it would be :
    --------------------------------------------
    /*fills an array with 8's*/
    char array[SIZE], *p;
    for(p = &array[0]; p < &array[SIZE - 1]; )
    *p++ = 8;

    and why a char and not an int if I'm loading it with integers?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Does this mean that if I wanted to load every element of the array with the number 8 it would be
    Actually, if you put in just 8 it would place the ASCII character represented by 8, for a character you would have to write '8', inside the single quotes.

    >and why a char and not an int if I'm loading it with integers?
    The array can be of any legal type as long as you're also loading it with a legal type. It really doesn't matter since the pointer only goes by the address of each element.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    26
    Sorry - maybe my question wasn't clear.

    I did want to put 8 in every element. I realize that the computer will store this as an ascii number but my program will require number of + to - 10000. I will be adding number to specific elements but I wanted to preload the entire array with a value.

    Just to be clear - Did I modify your lines of code properly to cause 8 to be loaded in each element?

    Cheesehead

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Did I modify your lines of code properly to cause 8 to be loaded in each element?
    No, to do that you need to change the array declaration from char array[SIZE] to int array[SIZE].

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM
  5. Array of pointers
    By falconetti in forum C Programming
    Replies: 5
    Last Post: 01-11-2002, 07:26 PM