-2

I have to write a main function which uses a loop and pointer arithmetic to print the values of an array, but am not sure how to do this.

I have got the following so far, which may not be correct:

#include <stdio.h>

int *ptr;
ptr = &my_array[0];

int main (void)
{
  my_array[] = {1,23,17,4,-5,100};
  ptr = &my_array[0];
}

any help would be much appreciated. Thanks

Joe Perkins
  • 261
  • 3
  • 9
  • 17
  • 2
    This is a fairly simple thing to do, I would advise getting a good book and starting from there if you are unsure on how to do something this basic. http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – Daboyzuk Apr 26 '13 at 09:23
  • I only just started the course sorry. – Joe Perkins Apr 26 '13 at 09:26
  • @Daboyzuk please refer to the answer of One Man Crew. I think that one is you're looking for. – mr5 Apr 26 '13 at 09:52

2 Answers2

1

you can use pointer like this

#include <stdio.h>

int *ptr;


int main (void)
{
  int my_array[] = {1,23,17,4,-5,100};
   ptr=my_array;//you can use ptr++ to get next pointer

 for(i=0;i<size_of_array;i++)
{

   printf("%d\n",*(ptr));
   prt++;
}
}
One Man Crew
  • 9,420
  • 2
  • 42
  • 51
0
int *x=my_array;    
int i;
for(i=0;i<size_of_array;i++)
{

       printf("%d\n",*(x+i));
 }
akp
  • 1,753
  • 3
  • 18
  • 26