1

I'm having trouble with this one part of my program dealing with dynamic arrays in C. My teacher provided the prototype functions. I am stuck on the appending part. I just don't understand why p is a double pointer. At first I thought maybe we are suppose to create array of dynamic arrays but then the deletion function does not need a double pointer. This is what I have so far:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

/*
Name: Marcus Lorenzana
Assignment: HW 4
*/

struct _dynamic
{

    int size;
    int max_size;
    int *data;

};
typedef struct _dynamic dlist_t;

dlist_t * make_dlist(int size);
void app_dlist(dlist_t **p, int value);
int ins_dlist(dlist_t **p, int val, int index);
int del_dlist(dlist_t *p, int index);

int main(int argc, char * argv[])
{

    dlist_t *plist;
    plist = make_dlist(50);



    return 0;
}

dlist_t *make_dlist(int size)
{

    dlist_t *list;
    list = (dlist_t *) malloc (sizeof(dlist_t)); 

    list->size = 0;
    list->max_size = size;

    list->data = (int *) malloc((sizeof(int))*size);


    return list;

}

void app_dlist(dlist_t **p, int value)
{
    /*
    int size;
    size = p->size;

    p->data->size = value;

    size++;
    p->size = size; 
    */



}

int ins_dlist(dlist_t **p, int val, int index)
{

}

int del_dlist(dlist_t *p, int index)
{

}
1
  • 1
    You don't need to cast the return value of malloc in a C program. Commented Jun 21, 2013 at 20:19

1 Answer 1

1

If you have a dynamically-sized array, it's possible that on append you will need to reallocate memory. In order for you to update the caller's pointer, you have to pass it by reference. In C, that means passing a pointer (to a pointer, in this case).

On deletion, you don't need to update the caller's pointer, so only the single * is necessary.

You should read the comp.lang.c FAQ question 4.8, and probably all of section 4 to get more comfortable with pointer semantics.

Sign up to request clarification or add additional context in comments.

7 Comments

Except in this case, dlist_t is a struct which contains a pointer to the data. So a dlist_t ** is a pointer to a pointer to a struct which contains a pointer to data. That's one level of indirection more than necessary.
Good catch - didn't notice that. I'm at a loss in that case. Maybe the prof changed his mind about what the data structure should be?
Probably. I would just use a single pointer, Marcus, and then ask your teacher about it. Maybe you'll get extra credit points for pointing out his/her error. :)
Perhaps, the only thing is in the powerpoint that describes the prototype function he states on this particular function "Why is the first argument a double pointer?"
Okay this is what he said: You can do it without the pointer to a pointer because append may need to change the pointer to the data area, but it should not have to change the pointer to the dlist struct itself. The syntax I showed applies where the whole thing you point to might be reallocated, such as in a linked list.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.