2013-06-18 2 views
0

2 차원 문자 배열에 액세스하려고합니다. 내가 올바른 주소에 포인터를 가지고 있지만 어떻게 든 dreferencing 작동하지 않습니다.C 포인터를 통해 2D 문자 배열에 액세스

char ary[5][8]; 
char temp[8]; 
int i; 

char **a_ptr = &ary; 

    for(i=0; i<5; i++){ 
     sprintf(temp, "0x10%d" , i); 
     strcpy(ary[i] , temp); 
     printf("~~~%[email protected]%x == 0x%x" , ary[i] , &ary[i] , (a_ptr + i)); 

    } 

    for(i=0; i<5; i++){//This wont work. 
     printf("~~~%[email protected]%x" , *(a_ptr + i) , (a_ptr + i)); 

    } 

다음은 포인터를 분리하기 전에이 fucniton의 출력입니다.

출력 형식 : 값 우리 배열 값이 정확하게 채워지고 a_ptr는 주소 (& 진 [I] == (a_ptr 정정 가리키고 있는지 출력 이상에서 볼 수 있듯이 @ 주소

[email protected] == 0x5fbff408 
[email protected] == 0x5fbff410 
[email protected] == 0x5fbff418 
[email protected] == 0x5fbff420 
[email protected] == 0x5fbff428 

I +)).

그러나 포인터가 추론 일 때 거기에서 부서집니다. [] 연산자를 사용하더라도 동일합니다.

*(a_ptr + i); //Breaks 
a_ptr[i]; //Breaks as well 

그러나 (a_ptr + i)는 올바른 주소를 가리 킵니다.

감사합니다,

답변

4

char **a_ptr = &ary; -이 이해되지 않는다. char** a_ptr은 포인터에 대한 포인터입니다. 필요한 것은 배열에 대한 포인터입니다. 다음은 수행합니다

char (*a_ptr)[8] = ary; // also don't take the address of the array 

당신이 포인터 주소를 인쇄하려는 경우, printf%p의 형식을 가지고있다. 0x%x%p으로 바꿉니다. 다음과 같이

나는 코드를 편집하지했습니다 내 컴파일러는 더 이상 경고를 발행합니다

#include <stdio.h> 
#include <string.h> 

int main() 
{ 
    char ary[5][8]; 
    char temp[8]; 
    int i; 

    char (*a_ptr)[8] = ary; 

    for(i=0; i<5; i++) 
    { 
     sprintf(temp, "0x10%d" , i); 
     strcpy(ary[i] , temp); 
     printf("~~~%[email protected]%p == %p" , ary[i] , &ary[i] , (a_ptr + i)); 

    } 

    for(i=0; i<5; i++) 
    { 
     printf("~~~%[email protected]%p" , *(a_ptr + i) , (a_ptr + i)); 
    } 

    return 0; 
} 

그리고 지금 내 출력은 다음과 같습니다

$ ./a.out 
[email protected] == [email protected] == [email protected] == [email protected] == [email protected] == [email protected][email protected][email protected][email protected][email protected] 

이 당신이 얻을 바라고 무엇입니까?

포인터 만없이 배열에 의존하는 일부 코드 :

#include <stdio.h> 
#include <string.h> /* for strcpy */ 
#include <stdlib.h> /* for free and malloc */ 

int main() 
{ 

    char** two_d = malloc(sizeof(char*) * 5); /* allocate memory for 5 pointers to pointers */ 

    int i; 
    for(i = 0; i < 5; i++) /* for each of the five elements */ 
    { 
     two_d[i] = malloc(2); /* allocate memory to each member of two_d for a 2 char string */ 

     strcpy(two_d[i], "a"); /* store data in that particular element */ 

     printf("%s has address of %p\n", two_d[i], &two_d[i]); /* print the contents and the address */ 

     free(two_d[i]); /* free the memory pointer to by each pointer */ 
    } 

    free(two_d); /* free the memory for the pointer to pointers */ 

    return 0; 
} 
+0

네, 잘 작동하고 출력이 예상 정확히 것입니다. 그러나 array (char * a_ptr [8])에 대한 포인터를 사용하고 싶지는 않으며 포인터와 동일한 포인터를 사용하고 싶습니다. 좋습니다 dereferencing에 문제가 있습니다. \t printf ("~~~ % s @ % p", (a_ptr + i), (a_ptr + i)); 값보다 '*'연산자가없는 기준에 액세스 할 수있는 경우 솔직하게, 나는 그것이 왜 있는지 전혀 모른다. * (a_ptr + i) 구문이 작동해서는 안됩니까? 또는 a_ptr [i]도 잘 작동해야합니다. – ila

+0

'char ary [5] [8]'는 단지 40 개의 문자와 포인터를 할당하지 않습니다. 포인터에 대한 포인터를 원한다면,'char * ary [5]'를 선언 한 다음, 8 개의 문자와 다른 포인터로 5 개의 포인터를 초기화하십시오. 포인터는 배열이 아닙니다. –

+0

그러면 아래의 설명이 효과가 있습니다. \t char ** a_ptr = &ary; \t printf ("~~~ % s @ % p", (a_ptr + i), (a_ptr + i))); 값이 올바르게 표시됩니다. 하지만 여전히 역 참조가 작동하지 않습니다! – ila

관련 문제