简体   繁体   中英

C Access 2D char array via Pointer

I am trying to access a 2D array of chars. I have a pointer on right address but somehow dreferencing is not working.

    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("~~~%s@0x%x == 0x%x" , ary[i] , &ary[i] , (a_ptr + i));

    }

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

    }

Below is the output of this fucniton before it breaks to derefence the pointer.

Output Format : Value@Address

0x100@0x5fbff408 == 0x5fbff408
0x101@0x5fbff410 == 0x5fbff410
0x102@0x5fbff418 == 0x5fbff418
0x103@0x5fbff420 == 0x5fbff420
0x104@0x5fbff428 == 0x5fbff428

As we can see in above output that array values are filled correctly and a_ptr is pointing to correct addresses (&ary[i] == (a_ptr + i)).

But when the pointer is deference then it breaks there. Even using the [] operators does the same.

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

However, (a_ptr + i) points to the right address.

Thanks,

char **a_ptr = &ary; - this makes no sense. char** a_ptr is a pointer to a pointer. What you need is a pointer to an array. The following will do:

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

If you're trying to print the pointer address, printf has a format for this %p . Replace your 0x%x s with %p .

I've edited your code as follows and my compiler no longer issues warnings:

#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("~~~%s@%p == %p" , ary[i] , &ary[i] , (a_ptr + i));

    }

    for(i=0; i<5; i++)
    {
        printf("~~~%s@%p" , *(a_ptr + i) ,  (a_ptr + i));
    }

    return 0;
}

And now my output is:

$ ./a.out 
~~~0x100@0xbfc77a74 == 0xbfc77a74~~~0x101@0xbfc77a7c == 0xbfc77a7c~~~0x102@0xbfc77a84 == 0xbfc77a84~~~0x103@0xbfc77a8c == 0xbfc77a8c~~~0x104@0xbfc77a94 == 0xbfc77a94~~~0x100@0xbfc77a74~~~0x101@0xbfc77a7c~~~0x102@0xbfc77a84~~~0x103@0xbfc77a8c~~~0x104@0xbfc77a94

Is this what you were hoping to get?

Some code that relies only on pointers and no arrays:

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM