accessing arrays with pointers

This is a discussion on accessing arrays with pointers within the C Programming forums, part of the General Programming Boards category; Hi all! I am new here, and I am also new to C language... I just had a few classes ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    37

    Question accessing arrays with pointers

    Hi all! I am new here, and I am also new to C language... I just had a few classes in Fortran and also know some Unix basics, but none of these seem to help me at the moment.

    I have to write a function which will calculate a sum of all the elements in an array[5], but I have to do it using pointers to access these elements.... I feel so confused about it all. Can you help me get through this?



    I thought that this would be a way out, but there are no pointers there.

    /code/:
    Code:
    int sum_array(int array[], int total_sum)
    {
    int i;
    for (i=0; i<=5; i++)
    total_sum += array[ i ]; // There are really no spaces around the i but it makes it italics here - Done by Kermi3 
    }
    Even when I tried this, the compiler (Visual Studio) said there is an error....
    Can you help, please?
    Thanks...

    Code tags added by Kermi3, good attempt on it....see http://www.cprogramming.com/cboard/s...threadid=13473 to get it right next time or PM me

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I thought that this would be a way out, but there are no pointers there.
    Ah, but there are pointers, you just can't see them. This is the equivalent function with pointer notation (what those nice little [] operators hide from you) and the problems fixed:
    Code:
    int sum_array ( int *array, int size )
    {
      int i, total_sum = 0;
    
      for ( i = 0; i < size; i++ )
        total_sum += *(array + i);
    
      return total_sum;
    }
    This is a reasonable implementation if you must use pointers, but I think your problem is that you must use pointers in place of the i variable. The only safe way is to have a sentinel value at the end of the array, declared like this:

    int a[6] = { 1, 2, 3, 4, 5, -1 };

    And the function could be modified like so:
    Code:
    int sum_array ( int *array )
    {
      int *i, total_sum = 0;
    
      i = array;
    
      while ( *i != -1 ) {
        total_sum += *i;
        ++i;
      }
    
      return total_sum;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    In general you access an array with pointers like this. These two are equivalent.

    bob = array[4];
    bob = *(array + 4);

    Here is your code:
    Code:
    #include <stdio.h>
    
    int add(int a[])
    {
       int total = 0;
       for (int i=0;i<=4;i++){
    	total += *(a+i);}
    	return total;
    }
    int main (){
    	int array[4];
       array[0] = 1;
       array[1] = 2;
       array[2] = 3;
       array[3] = 4;
       array[4] = 5;
       int bob = add(array);
       printf("Total is:%d",bob);
       return 0;
    }
    Hope this helps
    http://uk.geocities.com/ca_chorltonkids

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    I'm only new to this myself so my code was almost certainly not perfect. I can only help to the best of my ability and I figure considering people help me I should at least try to help others.
    "inline variable declarations are presently only in C++" I don't even know what this means. It compiled alright so I figured it was okay.
    http://uk.geocities.com/ca_chorltonkids

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>"inline variable declarations are presently only in C++"
    Means you cannot declare a variable in the middle of a section of code.

    This is how the code should be structured:
    Code:
    int foo(void)
    {
        /* Declare variables here */
       int i;
       char myname[100];
    
       /* Now do code.  No more declaring allowed. */
       ....
    }
    However, the latest standard of C supports this, which is possibly why your compiler didn't complain.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > which is possibly why your compiler didn't complain.
    Or as seems more likely, was actually compiled as C++ (vc++ for example defaults to C++)

    > I figure considering people help me I should at least try to help others.
    I applaud you for this - it's always nice when more people contribute.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    Thanks to all who replied... I tried some suggestions, and I still get warnings, like "different levels of indirection", "actual parameter different from formal", or the program crashes.
    I'll work on this a little more and I'll get back to you...

    What actually means "different levels of indiection"? Is this the reason for crashing?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I tried some suggestions, and I still get warnings
    You probably aren't using the suggestions correctly. Unless code I post is tagged at the top with a /* pseudocode */ comment, I've compiled it and run it successfully on two compilers and de-linted it to my satisfaction. Most of the other members do the same (I hope!). Could you post what you have so we can diagnose what you are doing wrong?

    >Is this the reason for crashing?
    Probably, if it lets you compile and run such erroneous code.

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

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    This is the code then. I started writing this program, and I still have a long way to go before I finish ...
    I made some more changes and I still get 3 warnings:
    (118) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'int '
    (118) : warning C4024: 'store_1totals' : different types for formal and actual parameter 1

    I know, I must have messed up when trying to get it into the program
    Thanks!
    Last edited by Nutka; 09-30-2002 at 05:10 PM.

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    Thank you, thank you, thank you! I couldn't sleep for the last 2 weeks because of this arrays/pointers business. I think everything will get clearer very soon

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with returning arrays using pointers
    By cuba06 in forum C Programming
    Replies: 9
    Last Post: 11-23-2007, 10:40 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 03:31 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. pointers and arrays..
    By ahming in forum C Programming
    Replies: 1
    Last Post: 04-24-2004, 04:12 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM

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