# Thread: accessing arrays with pointers

1. ## 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. >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);

}```
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;
}

}```
-Prelude

3. 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 total = 0;
for (int i=0;i<=4;i++){
total += *(a+i);}
}
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

4. 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.

5. >>"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.

6. > 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. 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. >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

9. 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!

10. 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