3

I want my struct to carry a string. I defined it like so:

typedef struct myStruct {
  char* stringy
} myStruct

and a function

free(char *stringy){
   //structObj is a struct object
   structObj->stringy = stringy
}

Is this correct? I have a feeling that since it's a local variable, stringy will be lost, and the pointer will point to garbage.

Sorry, new with C.

10
  • your statement is correct, but does not match the code you have posted. Commented Sep 4, 2013 at 3:53
  • Oh Yeah, sorry, I kinda edited this in a hurry. edited Commented Sep 4, 2013 at 3:54
  • 9
    a function called "free" is probably a bad idea. Commented Sep 4, 2013 at 3:55
  • @Duck Yes totally, but I am purposefully redefining free. Commented Sep 4, 2013 at 3:56
  • 2
    @Secret It is still a bad idea... Commented Sep 4, 2013 at 3:57

5 Answers 5

2

It would be garbage if you were somehow using char** stringy, but structObj->stringy = stringy means "you know the thing that stringy points to? Now structObj->stringy points to that". Of course, it is still possible to unset the value which the pointer is pointing to, and at that point dereferencing will yield garbage.

Here's an example to make it clearer:

 #include<stdio.h>
 typedef struct mStruct {
  char* stringy;
 } myStruct;

 myStruct * structObj;

 void doSomething(char* stringy)
 {
      structObj->stringy = stringy;
 }

 int main(int argc, char* argv)
 {
      char* a = "abc\n";
      structObj = malloc(sizeof(myStruct));
      doSomething(a);
      a = "qxr\n";
      printf(structObj->stringy);
 }// prints "abc\n"
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for being finicky but I believe that you should have gotten a compiler error/warning for the string literal assignment to a non const char pointer.
@nonsensical I do get a compiler warning, but it served as an example.
1

If stringy is defined in callers of free function, as long as they keep the actual string in its place (where stringy points), no problem.

Comments

1

There is not any local variable declaration in your code.

You have to declare:

typedef struct myStruct {
  char* stringy
} myStruct;

free(char *stringy){
   myStruct *structObj;
   structObj->stringy = stringy;
}

Pay attention to the semicolon that I've added to the end of the typedef declaration.
This was not not in your code.

The object structObj is a struct whose type is myStruct.

Now, your parameter stringy comes from another site, it is not lost.
But the struct structObj will have duration only inside your "free" function.

EDIT

I have fixed an error: the right declaration has to be "pointer to structObj", which is done in this way:

  myStruct *structObj;

Observe that now myStruct is a non-initialized pointer, so the following assignment is legal:

  structObj->stringy = stringy;

but will not work.

However I think this goes beyond the scope of the original question...

Comments

0

myStruct is type which you defined for your struct myStruct .that to you need to create an object before using.

you need to do like this:

typedef struct myStruct {
  char *stringy;  
} myStruct_t; //user defined  data type  

 myStruct_t *obj;  

// you need to allocate memory dynamically.   
 obj= (myStruct_t *) malloc(sizeof(myStruct_t)); 

usage:

scanf("%s",obj->stringy);

printf("%s",obj->stringy);

in function:

  my_free(char *str)  //str is local string 
    {
     obj->stringy=str;
    }

your can also try this code :

typedef struct myStruct {
  char stringy[20]; //char *stringy  
} myStruct_t; //user defined  data type

myStruct_t obj; //object creation    

usage:

scanf("%s",obj.stringy);

printf("%s",obj.stringy);

in function:

   my_free(char *str)  //str is local string 
    {
       strcpy(obj.stringy,str);
    }

Comments

-1

You're correct that as soon as what it points to goes out of scope, it will point to garbage: this is a dangling pointer. You'll need to allocate some memory and perform a copy to fix this:

add_string(my_struct* s, const char* c)
{
    size_t len = strlen(c);
    s->file = malloc(len + 1);
    strcpy(s->file, c);
}

Don't forget that you'll need to free it when you're done:

void destroy_struct(my_struct* s)
{
    free(s->file);
    free(s);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.