0

I'm trying to dynamically allocate memory to a char pointer using malloc() but not sure what I'm missing here.

void item(struct product *pr, const char *title, double price)
{   
  title = malloc((strlen(title)+1) * sizeof(const char));
}

I added the +1 for the /0 character, not sure if needed, but didn't work without it either. I assume I don't need to allocate memory for the structs or double.

liquardo
  • 91
  • 1
  • 10
  • What do you expect to happen, and what exactly doesn't work? – aschepler May 05 '15 at 17:02
  • 2
    If `title` already has contents, why are you assinging new memory to that variable? – crashmstr May 05 '15 at 17:05
  • There are tests used by netbeans and after I run the tests, it says "Memory for title string not dynamically allocated". Now I'm extremely new to C and from what I've read this seems to be along the lines of what I need in order to allocate space for the possible constructor, so not sure what the problem is. – liquardo May 05 '15 at 17:05
  • 1
    There is not point allocating memory to a `const char *` pointer, since the compiler will not let you write to the memory allocated. – Weather Vane May 05 '15 at 17:08
  • 2
    You are not showing enough context of what you are trying to do here or how this is called and what the expected result will be. – crashmstr May 05 '15 at 17:09
  • The goal is to initialize this structure so I can make a method for adding an item afterwards. A guide I'm trying to follow says "The title field is just a pointer to a string, that needs to be separately allocated" and then the error I posted above led me to believe that I had to allocate memory for this pointer. I guess I am very wrong then. – liquardo May 05 '15 at 17:13
  • 1
    I guess that `char *title` should be a field in your structure. You would allocate memory for the `struct` and also for the string it contains. – Weather Vane May 05 '15 at 17:16
  • 1
    @FroZenL1Qu1d `pr->title = malloc(...)` is very different from `title = malloc(...)`. – crashmstr May 05 '15 at 17:31
  • oh wow, that's the one. I'm a day into C, so having a hard time explaining myself. Thanks a lot mate. – liquardo May 05 '15 at 17:33

1 Answers1

0

I'm trying to dynamically allocate memory to a char pointer using malloc()
Actually, you are attempting to allocate memory to a const pointer to char.

The const keyword by definition prevents changes to a variable beyond its initial creation (declaration/definition). The fact that this is passed as an argument:

void item(struct product *pr, const char *title, double price)  

requires that title has already been defined (as an array of strings it appears) before it was passed in the calling function.

As written, and assuming the following variable descriptions, your function would accept the following:

const char *title={"Call of the Wild"};

typedef struct {
    int something;
}PR;

PR pr;

.../in some function...
item(&pr, title, 3.2);//because title is passed as const, it cannot be changed
ryyker
  • 22,849
  • 3
  • 43
  • 87