Ive got this code but it won't run and i can't find any more errors
int main(){
int *p, i, a[10];
p=a;
for (i=0; i<10; i++)
scanf("%d", p++);
printf("\n");
for (i=0; i<10; i++)
printf("a[%d]=%d\n", i, *p++);
return 0;
}
the formatting is terrible, but my best guess is the line with *p++ .
but i consider myself a beginner still in c++ so don't take my word for it.
maybe add parenthesis around it like so: (*p)++;
int main(){
int *p, i, a[10];
p=a;
for (i=0; i<10; i++)
scanf("%d", p++);
printf("\n");
p = a; // make p point to the beginning of the array
for (i=0; i<10; i++)
printf("a[%d]=%d\n", i, *(p++));
return 0;
}
Note:
The topic of this thread is "C++", but this is merely C.
Pointers are error prone. You might want to consider using the subscript operator instead.
1 2 3 4 5 6 7 8 9 10 11
#include <stdio.h>
int main(){
int i, a[10];
for (i=0; i<10; i++)
scanf("%d", &a[i]);
printf("\n");
for (i=0; i<10; i++)
printf("a[%d]=%d\n", i, a[i]);
return 0;
}
int main()
{
constint NUM_ELEMS = 10 // #define NUM_ELEMS 10
int *p, i, a[NUM_ELEMS];
p = a; // same as p = &a[0]
for (i=0; i < NUM_ELEMS; i++)
scanf("%d", p++);
printf("\n");
// Reset p to the beginning
p = a;
for (i=0; i < NUM_ELEMS; i++)
printf("a[%d]=%d\n", i, *p++);
getch();
return EXIT_SUCCESS;
}