Thread: Arrays and Pointers in a loop

  1. #1
    Registered User
    Join Date
    May 2013
    Location
    Rishon Le Siyon, Hamerkaz, Israel, Israel
    Posts
    13

    Post Arrays and Pointers in a loop

    Hey guys long time since i've been posting something around here (not.)

    So I got to the chapter on how Arrays and Pointers can work together and I want to try my skills as a beginner to code a program.

    THE TARGET OF THE PROGRAM:

    Set an Array (a[10]) and a Pointer to that array (*pa) and code a loop (for( ; ; loop) that will advance that pointer (*pa) and will set a new content into it with each loop. that means that in the end of the day, my program will automatically set content to each cell of the array by promoting the pointer by 1 and add the sum to that pointer.

    THE PROBLEM:

    When I run the program it prints the address of the cells instead the value of it.

    THE CODE:

    Code:
    #include <stdio.h>
    
    void main()
    {
        float a[11], *pa;    //  Array and ptr set.
        int i;    // counter for the loop.
    
    
        pa = a; 
        
        for (i=0 ; i<=10 ; i++)    // the loop itself.
        {
            *pa = 50*1.5; 
    
    
            /* %d stands for the number of the counter
             for order.  %f = pa[i], well... you got it :) */
    
    
            printf("#%d equals to: #%-15.2f#\n", i, pa[i]);    
    
    
            /*advance the pointer pa (which is pa[0] atm) by 1
            to add value to the next in the array order. */
    
    
            pa++;    
            
        }
        putchar('\n');
    }
    Last edited by Or Ellenbogen; 06-27-2013 at 10:45 AM. Reason: spelling

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Here's what I get:
    Code:
    $ make arr
    gcc -Wall -ggdb3 -std=c99 -o arr arr.c
    arr.c:3:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
    cagarvin@cagarvin-goobuntu:~/sandbox/cprogramming$ ./arr 
    #0 equals to: #75.00          #
    #1 equals to: #0.00           #
    #2 equals to: #0.00           #
    #3 equals to: #0.00           #
    #4 equals to: #0.00           #
    #5 equals to: #0.00           #
    #6 equals to: #0.00           #
    #7 equals to: #0.00           #
    #8 equals to: #0.00           #
    #9 equals to: #0.00           #
    #10 equals to: #0.00           #
    First, it's int main(), not void main. And you should return a value at the end of main, usually 0 for success (or EXIT_SUCCESS, which is defined in stdlib.h). If your book is teaching void main, get a newer one that teaches the current standard. If your compiler accepts void main, get a new one that adheres to the current standard.

    Second, you're not following the directions. They say make an array of length 10, but yours is 11. What you should do:
    Code:
    #define ARR_LEN    10
    ...
    int a[ARR_LEN];
    ...
    for (i = 0; i < ARR_LEN; i++)
    Note, the array is declared with 10 elements, not 11, and I use a < instead of a <= in the for loop. Also, defining a constant makes it easy to change the length of the array later simply by changing the definition of the constant. At the same time, all your loops that iterate through the array will also automatically change to loop the correct number of times.

    That being said, it's not the address you're seeing, it's random stuff in memory. You keep incrementing pa to point to the next spot in the array, but you're indexing it as well. If you have a pointer, doing pa[i] is the same as doing *(pa + i). So as you loop, you keep incrementing pa, starting at &a[0], then to &a[1], &a[2], etc. You're pointing to the middle of the array, but then indexing as well, and accessing data outside the array bounds. Instead of printing pa[i], just print *pa instead. It will refer to the right spot in the array.

    One more quick note, it might help to make each spot in the array contain a different value, so you know your loop worked correctly. Try incorporating i into your equation, like
    Code:
    *pa = 3.45 * i;

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The program is not working because it increments pa and also uses pa[i]. Use one method or the other, but not both at the same time.

  4. #4
    Registered User
    Join Date
    May 2013
    Location
    Rishon Le Siyon, Hamerkaz, Israel, Israel
    Posts
    13
    Thank you guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers in a for loop
    By Edelweiss in forum C Programming
    Replies: 6
    Last Post: 09-16-2011, 01:38 AM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  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. pointers, arrays, for loop, class
    By Lucid003 in forum C++ Programming
    Replies: 7
    Last Post: 10-18-2005, 04:36 PM
  5. pointers in arrays... Declaring objects in a loop.
    By Guest852 in forum C++ Programming
    Replies: 10
    Last Post: 04-05-2005, 06:57 AM

Tags for this Thread