Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 声明和访问结构内部int数组的指针_C_Arrays_Pointers_Struct - Fatal编程技术网

C 声明和访问结构内部int数组的指针

C 声明和访问结构内部int数组的指针,c,arrays,pointers,struct,C,Arrays,Pointers,Struct,下面是我的代码 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访问这些值 当我尝试最脚时,它会抛出一个错误。我知道如何访问数组指针,但由于指针位于结构内部,这让我很恼火。欢迎提供任何指导。以下是工作代码: struct foo

下面是我的代码

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访问这些值

当我尝试最脚时,它会抛出一个错误。我知道如何访问数组指针,但由于指针位于结构内部,这让我很恼火。欢迎提供任何指导。

以下是工作代码:

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;
}
structfoo
{
int*ptr;
int size;//等于数组中的元素
}足部的;
int main()
{
int i;
int a[5]={1,2,3,4,5};
最英尺。ptr=a;

对于(i=0;i这似乎是一个关于指针及其与数组的关系的一般性问题。我已经有一段时间没有广泛使用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
fooTest.ptr[0]
为什么需要双指针@ANBUSANKAR@user2733436不需要双指针…只需删除
&
符号…肯定会起作用…你让事情变得更糟。现在你正在泄漏内存,并在自动数组上调用
free
(这会导致未定义的行为)@MattMcNabb现在是fyn吗?
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