简体   繁体   中英

What is the difference between these access in C?

i started learning C today, and i have some questions about accessing a pointer data.

I have this function in C:

typedef struct
{
    size_t size;
    size_t usedSize;
    char *array;
} charList;

void addToCharList(charList *list, char *data)
{
    if(list->usedSize == list->size)
    {
        list->size *= 2;
        list->array = realloc(list->array, list->size * sizeof(int));
    }
    list->array[list->usedSize++] = *data;
    printf("1: %d\n", *data);
    printf("2: %s\n", data);
    printf("3: %p\n", &data);
}

I use it to create a "auto growing" array of chars, it works, but i'm not understanding why i need to attribute the value "*data" to my array. I did some tests, printing the different ways i tried to access the variable "data", and i had this outputs (I tested it with the string "test"):

1: 116
2: test
3: 0x7fff0e0baac0

1: Accessing the pointer (i think it's the pointer) gives me a number, that i don't know what is.

2: Just accessing the variable gives me the actual value of the string.

3: Accessing it using the "&" gets the memory location/address.

When i'm attributing the value of my array, i can only pass the pointer, why is that? Shouldn't i be attributing the actual value? Like in the second access.

And what is this number that gives me when i access the pointer? (First access)

So, in the first printf, you're not actually accessing the pointer. If you have a pointer named myPointer , writing *myPointer will actually give you access to the thing that the pointer is pointing to. It is understandable to be confused about this, as you do use the * operator when declaring a variable to specify that it is a pointer.

char* myCharPtr; // Here, the '*' means that myCharPtr is a pointer.

// Here, the '*' means that you are accessing the value that myCharPtr points to.
printf("%c\n", *myCharPtr); 

In the second printf, you're accessing the pointer itself. And in the third printf, you're accessing a pointer to a char pointer. The & operator, when put before a variable, will return a pointer to that variable. So...

char myChar = 'c';

// pointerToMyChar points to myChar.
char* pointerToMyChar = &myChar;

// pointerToPointerToMyChar points to a pointer that is pointing at myChar.
char** pointerToPointerToMyChar = &pointerToMyChar;

When you're trying to store the value in the array, it's forcing you to do *data because the array stores chars, and data is a pointer to a char. So you need to access the char value that the pointer is pointing to. And you do that via *data .

Lastly: the reason that printf("1: %d\n", *data);prints a number is that chars are secretly (or not secretly) numbers. Every character has a corresponding numeric value, behind the scenes. You can see which numerical value corresponds to which character by looking at an ascii table.

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