2014-10-01 143 views
1

以下是我的代碼聲明和訪問指針的int數組內部結構

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)它會引發錯誤。我知道如何訪問數組指針,但由於指針位於結構體內部,所以它將我拋棄。任何指導將不勝感激。

+1

'* fooTest.ptr','fooTest.ptr [0]' – imreal 2014-10-01 03:43:02

+0

爲什麼需要雙指針@ANBUSANKAR – user1010101 2014-10-01 03:44:07

+0

@ user2733436無需雙指針...只是刪除'&'符號....當然它會工作... – 2014-10-01 03:50:16

回答

2

這裏工作代碼:

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

此代碼泄漏內存,刪除'malloc'行 – 2014-10-01 05:35:15

+0

@MattMcNabb我編輯和釋放內存'免費(fooTest.ptr);' – Rustam 2014-10-01 05:39:21

+0

你讓事情變得更糟。現在你正在泄漏內存,並且在自動數組上調用'free'(這會導致未定義的行爲) – 2014-10-01 05:40:30

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