본문 바로가기
카테고리 없음

[C] (double) Pointer

by HarryJang 2023. 7. 15.

목차

    When it comes to learning C language, the memory management is always the problem. You always have to take care of allocating and freeing the memory which is such a pain. But it's not only that. One of the notorious features of this language is a POINTER. Countless number of people using or learning C have hard time because of the pointer and I am one of them. ​

     

    Last term, I had OS course and the main language used was C. Another one was assembly but, thanks to god, we haven't really used them in this course. So, all those days I have been using python and, all of a sudden, I had to encouter this gentleman again in one of the (arguably)hardest course and as I've said, pointer became a problem soon enough. As the course's getting close to the end, I got an idea of pointer back in my brain. I don't want to lose it and do googling later, so I'll try to leave a record of my understanding on it. I mean there are lots of good tutorial on this so I don't recommend reading my writing to learn pointers X) Oh but I won't write all those basic things but especially ones for double pointers. For basic ideas, go https://www.youtube.com/watch?v=2ybLD6_2gKM 

    So what is double pointer? It can be called as pointer to a pointer. It could be coded like following.

     

    char *x;  // Memory locations pointed to by x contain 'char'
    char **y; // Memory locations pointed to by y contain 'char*'
    
    x = (char*)malloc(sizeof(char) * 100);   // 100 'char'
    y = (char**)malloc(sizeof(char*) * 100); // 100 'char*'

     

    So in here, x is a pointer and y is double pointer. When you allocate memory, for x, you allocate memory of size char. Whereas for y, you allocate memory of size char* which is a pointer. It sounds obvious but when you've been away from this concept for a long time or you have no energy to run your brain as usual, it can be quite confusing with many * and stuff. It's hard to multitask when you are focusing on the logic of the code and to figure out pointers at the same time. It is helpful to bare an idea in your mind as an image.

     

    So the first box represents the double pointer variable. It contains the address of a pointer it's pointing to. The difference between pointer and double pointer is the memory it's pointing to. The pointer is pointing to an integer. So we have to malloc(sizeof(int)). The double pointer is pointing to a pointer. So we have to malloc(sizeof(int *)). We have to malloc(sizeof(data type of a variable that we are pointing to)). I sometimes confuse that we need to allocate some space for a pointer when create pointers to integer. It's incorrect and we have to allocate space for the variable pointing to.

     

    I believe my writing here is so messy and hard to follow what I'm saying. It's just for my personal purpose recap so don't expect to understand what I've written haha. I recommend you watch the video that I have linked up there!