3

I Have a struct, and a constructor method for it. However, I am unable to create initialize the struct properly. In the parameters, I pass a pointer to that struct type, and all its instance variable(sorry for using java terminology, new to C). here is my code for the method.

 typedef struct anagramElement anagramElement;
 struct anagramElement {
    char* word;
    char* key;
 };

void createAnagramElement(char* const word, char* const key, anagramElement* rv)
{
    rv = (anagramElement*) malloc(sizeof(anagramElement));
    rv->word = word;
    rv->key = key;

}

In the main after, passing anagramElement *ptr, char *Word, char*key, when printing the elements data, segmentation fault occurs. NOTE:I Cannot change the parameters or the method return type.

3 Answers 3

6

You are passing rv by value. Anything you do to it won't "stick" beyond the function call. To avoid double pointers, refactor so createAnagramElement() returns the pointer.

anagramElement* createAnagramElement(char* const word, char* const key)
{
    // don't cast the result of malloc()
    anagramElement* rv = malloc(sizeof(anagramElement));
    if (rv != NULL) {
        // Just storing pointers to strings (not copies) so you need to be
        // sure the strings don't go out of scope...
        rv->word = word;
        rv->key = key;
    }
    return rv;
}

If you can't change the parameters (per comments), perhaps createAnagramElement() is supposed to initialize an already allocated anagramElement:

void createAnagramElement(char* const word, char* const key, anagramElement* rv)
{
    // Don't change value of rv, just change values in the struct.
    // rv = (anagramElement*) malloc(sizeof(anagramElement));
    // Should also NULL check rv, but ignoring for now.
    rv->word = word;
    rv->key = key;
}

You would call it like:

anagramElement    instance;
createAnagramElement("hello", "world", &instance);
Sign up to request clarification or add additional context in comments.

4 Comments

I cannot change the parameters of the function.
You are passing the value of a pointer. You change the value when you do the malloc. I'll edit to put in what might answer your question since you can't change params.
So I do not need to allocate memory for its key and word?
I can't tell you that - I don't have enough info. Short answer is you need to know that key and word won't be freed/go out of scope during the lifetime of your struct. Your struct has pointers to char strings and not actual instances. If there is a chance the strings might go out of scope, then you should copy them (perhaps with strdup) - just remember any memory you allocate you're going to have to free.
0
void createAnagramElement(char* const word, char* const key, anagramElement** prv)
{
    *rv = (anagramElement*) malloc(sizeof(anagramElement));

or

anagramElement* createAnagramElement(char* const word, char* const key)
{
    anagramElement *rv = (anagramElement*) malloc(sizeof(anagramElement));
    ...
    return rv;
}

Comments

0

Error handling is also important issue. While using double pointer, you can use a return value for result status like failed or success:

int createAnagramElement(char* const word, char* const key, anagramElement** element)
{
     *element= (anagramElement*) malloc(sizeof(anagramElement));

     if(*element == NULL){ return ANAGRAM_ELEMENT_FAILED; }

     //some operations

     return ANAGRAM_ELEMENT_SUCCESS;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.