以下是我的代码
struct foo
{
int *ptr;
int size;//equal to elements in array
}fooTest;
int main()
{
int a [5] = {1,2,3,4,5};
fooTest.ptr = &a;
return 0;
}
如何使用ptr获取值?
当我尝试fooTest。(* ptr)时会抛出错误。我知道如何访问一个数组指针,但由于指针位于一个结构中,它会让我失望。任何指导都将不胜感激。
答案 0 :(得分:1)
这是工作代码:
struct foo
{
int *ptr;
int size;//equal to elements in array
}fooTest;
int main()
{
int i;
int a [5] = {1,2,3,4,5};
fooTest.ptr=a;
for( i=0;i<5;i++)
printf("%d\n",fooTest.ptr[i]); // accessing the value here
return 0;
}
答案 1 :(得分:1)
这似乎是一个关于指针及其与数组关系的一般性问题。我已经有一段时间了,因为我广泛使用C(而不是C ++),但它是相同的wrt指针。下面是一些示例代码,可以提供有关指针使用的一般概念。
#include <stdio.h>
#include <malloc.h>
struct foo
{ int *ptr;
int size;//equal to elements in array
} fooTest;
int main()
{ int a [5] = {1,2,3,4,5};
int i;
// You can dynamically allocate memory for 5 ints like this:
// Note the cast to int *.
fooTest.ptr = (int*)malloc(5*sizeof(int));
// We cannot use cout in C, but printf is easier to format anyway.
printf("fooTest.ptr = %ld (The pointer itself, i.e. "
"an address in memory.)\n", fooTest.ptr);
printf(" a = %ld (Note different address than fooTest.ptr)\n", a);
printf("\n");
// Again, a and fooTest.ptr point to different arrays:
for(i=0;i<5;i++)
{ fooTest.ptr[i] = 11*(i+1);
printf("fooTest.ptr[%d] = %2d\n", i, fooTest.ptr[i]);
printf(" a[%d] = %2d\n", i, a[i]);
}
printf("\n");
// !!!!!!!!! THIS IS VERY IMPORTANT !!!!!!!!!!!!!!
// fooTest.ptr is the only link to the allocated block of memory.
// Don't loose it until it has been freed!
free(fooTest.ptr);
printf("fooTest.ptr = %ld fooTest still points to the same (now invalid)\n"
" location in memory after the allocated\n"
" memory has been freed.\n"
" I usually assign it to NULL to avoid\n"
" mysterious bugs.\n" ,
fooTest.ptr);
fooTest.ptr = NULL; // Avoid mystery bugs. If you attempt to
// access memory with a NULL pointer, the program
// will crash reliably.
printf("\n");
// a is a constant pointer to an array of integers.
// You can assign a pointer the value of another pointer.
// Sometimes you have to use a cast. It's best to be explicit
// with your intentions, but be careful not to cast to something
// that doesn't make sense.
fooTest.ptr = (int *) a;
printf("fooTest.ptr = %ld (The same value, since we assigned\n"
" a's value to fooTest.ptr\n",
fooTest.ptr);
printf(" a = %ld (Again, the pointer itself, \n"
" not the value it is pointing to.\n", a);
printf("\n");
// BTW, this would be an error, since a is a constant pointer,
// i.e. not a variable:
//a = (int *) fooTest.ptr;
// The [] operator dereferences the pointer with an offset,
// i.e. a[3] is the integer pointed to by a+3:
for(i=0;i<5;i++)
{ printf("fooTest.ptr[%d] = %ld\n", i, fooTest.ptr[i]);
printf(" a[%d] = %ld\n", i, a[i]);
}
printf("\n");
// Another dereference method: use the * operator.
// Think star, as in what is "stared" at a. (Have to be
// from the deep south for that to work).
for(i=0;i<5;i++)
{ printf("*(fooTest.ptr + %d) = %d\n", i, *(fooTest.ptr + i) );
printf("*( a + %d) = %d\n", i, *( a + i) );
}
// As long as we are talking about pointers, here is something folks
// get hung up on: the . operator vs the -> operator.
return 0;
} // End of main()
int moreAboutPtrs()
{ int a [5] = {1,2,3,4,5};
struct foo *fooTestPtr;
fooTestPtr = &fooTest;
//fooTestPtr .ptr = a; // Nope--error.
fooTestPtr ->ptr = a; // Yep. fooTestPtr is a pointer, so we have to use
// the awkward and hard to type -> operator.
(*fooTestPtr).ptr = a; // This also works ("the ptr member of the
// thing that fooTestPtr points to,
return 0;
} // End of moreAboutPtrs()
输出如下:
fooTest.ptr = 6609768 (The pointer itself, i.e. an address in memory.)
a = 1506428 (Note different address than fooTest.ptr)
fooTest.ptr[0] = 11
a[0] = 1
fooTest.ptr[1] = 22
a[1] = 2
fooTest.ptr[2] = 33
a[2] = 3
fooTest.ptr[3] = 44
a[3] = 4
fooTest.ptr[4] = 55
a[4] = 5
fooTest.ptr = 6609768 fooTest still points to the same (now invalid)
location in memory after the allocated
memory has been freed.
I usually assign it to NULL to avoid
mysterious bugs.
fooTest.ptr = 1506428 (The same value, since we assigned
a's value to fooTest.ptr
a = 1506428 (Again, the pointer itself,
not the value it is pointing to.
fooTest.ptr[0] = 1
a[0] = 1
fooTest.ptr[1] = 2
a[1] = 2
fooTest.ptr[2] = 3
a[2] = 3
fooTest.ptr[3] = 4
a[3] = 4
fooTest.ptr[4] = 5
a[4] = 5
*(fooTest.ptr + 0) = 1
*( a + 0) = 1
*(fooTest.ptr + 1) = 2
*( a + 1) = 2
*(fooTest.ptr + 2) = 3
*( a + 2) = 3
*(fooTest.ptr + 3) = 4
*( a + 3) = 4
*(fooTest.ptr + 4) = 5
*( a + 4) = 5