Hi, I am a relative newbie to C, although I am fairly experienced with Object Pascal. My question concerns pointers. First of all, I wrote the following test program:
Code:
#include <stdio.h>
typedef char *CharPtr;
CharPtr InitChar()
{
static char c;
c = 'a';
return &c;
}
int main ()
{
CharPtr c1;
c1 = InitChar();
printf("%c", *c1);
getchar();
return 0;
}
It compiled fine and the result "a" was printed on the screen. Now I wanted to modify my test program to use arrays instead of characters, so I wrote the following program:
Code:
#include <stdio.h>
typedef int IntArrayTen[10+1];
typedef IntArrayTen *ArrayPtr;
ArrayPtr InitArray()
{
static IntArrayTen a;
int i;
a[0] = 0;
for (i = 1; i <= 10; i++)
{
a[i] = i;
}
return &a;
}
int main ()
{
ArrayPtr aa;
aa = InitArray();
printf("%d", *(aa+4));
getchar();
return 0;
}
Now although I would expect this to print "4", instead it gave me some warnings and spat out a massive number. I found that I could solve the problem by changing to However I do not understand why this works, because then aa is a pointer to a pointer to an array and so would be a pointer, which is not a valid parameter for the printf function.
Hope you can help me here.
Adam.