0

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)

Toniotti
  • 77
  • 7
  • The 116 is the ASCII code of `t` , the character to which `data` is pointing . – M.M Dec 31 '20 at 03:14
  • Arrays cannot be passed by value in C; instead you can pass the address of where the first element of the array is found, and the recipient can then look up that address to access the array contents – M.M Dec 31 '20 at 03:14
  • You could also do `printf("1: %c\n", *data);` printing it as a character. – isrnick Dec 31 '20 at 03:47
  • In `*data` the `*` is the dereference/indirection operator, so `*data` return the value to which `data` points to, but in the example `data` is a pointer that points to the first character of the string `"test"`, the character `'t'` (which is equal to the number 116 when printed as an decimal integer). – isrnick Dec 31 '20 at 03:54
  • A few links that provide basic discussions of pointers may help. [Difference between char *pp and (char*) p?](https://stackoverflow.com/a/60519053/3422102) and [Pointer to pointer of structs indexing out of bounds(?)...](https://stackoverflow.com/a/60639540/3422102) (ignore the titles, the answers discuss pointer basics) Learning C isn't a race, or something you do in a semester, or year for that matter, it's more of a journey. There is a lot to learn, so slow down, pay attention to the details, and enjoy the journey. – David C. Rankin Dec 31 '20 at 04:27
  • Always `realloc()` using a temporary pointer. In the 2nd link above, see the `Location *addLocation ()` function for example. – David C. Rankin Dec 31 '20 at 04:34

1 Answers1

2

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.

EKW
  • 2,059
  • 14
  • 24
  • If you're interested in this kind of thing, I'd recommend reading [Joel Spolsky's article on what every programmer should know about unicode](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/) – EKW Dec 31 '20 at 04:19