0
main()
{
    struct MyList *list = NULL;

    int flag = MyListInit(list);
}

int MyListInit(MyList* list)
{
   list = malloc(sizeof(struct MyList));
   if (list != NULL) {
       return 1;
   }
   return 0;
}

After the function is called I want the list to hold a memory address which is allocated when malloc is called. But its value is NULL, but during runtime in the Mylistinit function, the value of list wont be NULL, but after returning, it will be changed back to NULL.

I do not want to return the address from function, I have to return an integer(firm on this)

kaylum
  • 13,833
  • 2
  • 22
  • 31
shreshta bm
  • 304
  • 4
  • 11
  • 1
    No; you need to pass the address of the pointer in the main program to the function, or you need to return the value from the function (but you've ruled that option out, so you need to pass the address of the pointer to the function, and change the function correspondingly). It would be best if you showed an MCVE ([How to create a Minimal, Complete, and Verifiable Example?](http://stackoverflow.com/help/mcve)). What you've got won't compile as presented, but it wouldn't take much to make it compilable. Note that missing the return type of `main()` has been invalid in C for about 15 years. – Jonathan Leffler Sep 01 '15 at 00:23
  • 1
    @erip: Not without a definition of the structure, but with a structure definition, it could. The layout is appalling, though — or was until someone was kind enough to fix it. – Jonathan Leffler Sep 01 '15 at 00:24
  • Nope, Even if I send the address, its being changed to NULL. – shreshta bm Sep 01 '15 at 00:26
  • Sorry about the layout, just copied and pasted a part of a 300 lines code. – shreshta bm Sep 01 '15 at 00:27
  • @shreshtabm Then you are doing it wrong. Show that code if you want it corrected. – kaylum Sep 01 '15 at 00:27
  • @Shreshta bm I think you are supposed to return 1 to indicate error and 0 to indicate success. – Moshe Rabaev Sep 01 '15 at 00:33
  • OT: It's `int main(void)` at least. – alk Sep 01 '15 at 05:24

1 Answers1

3

Since all variables are passed by value even pointers you must used a double pointer like this and You should cast to type (MyList*).

int MyListInit(MyList**); // function declaration
int main(){

   struct MyList *list = NULL;

   int flag = MyListInit(&list);
   return 0;
}

int MyListInit(MyList** list)
{
     *list = (MyList*)malloc(sizeof(struct MyList));
     if (list != NULL) return 1;
     return 0;
{
Moshe Rabaev
  • 1,892
  • 16
  • 31
  • I have to use int MyListInit(MyList* list) for this as I am overriding this from another location, cannot change the function definition. – shreshta bm Sep 01 '15 at 00:33
  • 1
    Note that the type in the question is `struct MyList`. This is C; without a `typedef struct MyList MyList;`, you can't omit the `struct`. If it were C++, the rules would be different. – Jonathan Leffler Sep 01 '15 at 00:37
  • Then there is no official way to allocate memory and assign to some pointer in c without the usage of double pointers because the double pointer is passed by value but that has no relation to the dereferenced version of the double pointer so you assign the allocated memory to the same address. Sorry – Moshe Rabaev Sep 01 '15 at 00:37
  • 1
    @shreshtabm: If you can't change the init function signature, you're hosed. I think you're misreading some rules — you wouldn't be asked to do the impossible. – Jonathan Leffler Sep 01 '15 at 00:37
  • Ya. I think I need to check with the prof about that. – shreshta bm Sep 01 '15 at 00:40
  • There is no need to cast the result of malloc/callo/reallc in C, nor is it recommended in any way. – alk Sep 01 '15 at 05:24