mkkhedawat
mkkhedawat

Reputation: 1717

Error while generating an array of pointer

Trying to implement below code for some assignment but getting an error for malloc array generation "[Error] conflicting types for 'stack'" Any Help ?? Thanks in Advance.

#include<stdio.h>
   #include<stdlib.h>

    struct treenode
    {
        char info;
        struct treenode *firstchild;
        struct treenode *next;
        int flag;
    };

    typedef struct treenode *NODEPTR;
    NODEPTR *stack;
    // Trying to create array here
    stack=(NODEPTR*)malloc(sizeof(NODEPTR)*20);


    int main()
    {
        printf("YO\n");
        return 0;
    }

EDIT :


I can't move it to main , as i have to access the stack globally in different functions. because Stack array gets destroyed when it go to another function. check here http://ideone.com/5wpZsp ,


When i give static declaration globally it works smoothly, here : http://ideone.com/3vx9fz

Upvotes: 2

Views: 107

Answers (4)

Louis Ricci
Louis Ricci

Reputation: 21086

Move your initialization of stack to inside of the main method.

EDIT An example showing how the malloc data can persist to other function calls even though malloc is called inside of main.

#include<stdio.h>
#include<stdlib.h>

struct treenode
{
    char info;
    struct treenode *firstchild;
    struct treenode *next;
    int flag;
};

typedef struct treenode *NODEPTR;
NODEPTR *stack;

void test_stack() 
{
    printf("%p\n", stack);
    printf("%d\n", stack[19]->flag);
}
int main()
{
    // Trying to create array here
    stack=(NODEPTR*)malloc(sizeof(NODEPTR)*20);
    stack[19] = (NODEPTR*)malloc(sizeof(struct treenode));
    stack[19]->flag = 42;
    test_stack();
    return 0;
}

Upvotes: 1

sujin
sujin

Reputation: 2853

#include<stdio.h>
#include<stdlib.h>

struct treenode
    {
    char info;
    struct treenode *firstchild;
    struct treenode *next;
    int flag;
    };
    typedef struct treenode *NODEPTR;
    NODEPTR *stack;
   int main()
   {

    stack=malloc(sizeof(NODEPTR)*20);

    printf("YO\n");
    return 0;
  }

This will work.

allocate memory inside(malloc) your main.

There is no need to typecast out put of malloc. for further info see this post

EDIT :

In your comment you mentioned that memory will be destroyed when you move other function.

This is not true. Once you allocate memory using malloc it will not be destroyed until you call free().

So if you want to access the malloc'ed variable in other function pass the variable as argument to other function.

See this example program below

    #include<stdio.h>
#include<stdlib.h>
#include <string.h>
char *str;
void passMalloc(char **str);

int main()
{
    str = malloc(100 * sizeof(char));
    strcpy(str, "GoodMorning");
    printf("\nMain before pass : %s\n", str);
    passMalloc(&str);
    printf("Main after pass : %s\n\n", str);
    free(str);
    return 0;
}

void passMalloc(char **str)
{
    strcpy(*str, "GoodEvening");
    printf("Function Def : %s\n", *str);
}

Upvotes: 0

John Bode
John Bode

Reputation: 123448

Step 1: Move the declaration of stack inside main. There's no reason it should be declared globally:

int main( void )
{
  NODEPTR *stack;
  ...

Step 2: Move the malloc call inside main (you cannot perform an assignment or a function call outside of a function).

Step 3: Drop the cast; it's unnecessary1 and just adds visual clutter.

Step 4: Use sizeof *stack as opposed to sizeof (NODEPTR) in the argument to malloc:

stack = malloc( sizeof *stack * 20 );

The result is the same, but this is easier to read, and avoids maintenance headaches if you ever change the type of stack.

Step 5: free your stack when you're done. Yeah, for this program it doesn't matter, but it's a good habit to get into.

So after all this, your code should read:

int main( void )
{
  NODEPTR *stack;
  stack = malloc( sizeof *stack * 20 );
  ...
  free( stack );
  return 0;
}

Stylistic nit: Hiding pointer types behind typedefs is bad juju IME. Pointer semantics are important, and if the programmer is ever expected to dereference an object of type NODEPTR (either explicitly, as (*node).info, or implicitly, as node->info), then it's usually best to declare that object using pointer declaration syntax, something like

typedef struct treenode Node;
Node *node;
Node **stack;
...
stack[i]->next = node->next;

etc. so the person using your types knows exactly how many levels of indirection are involved and can write their code accordingly (multiple indirection is not hard). If the type is meant to be truly opaque and never directly dereferenced, but just passed around to an API that handles all that, then hiding the pointerness of that type is okay. Otherwise, leave it exposed.

I tend not to typedef struct types for a similar reason, but I suspect I'm an outlier in that regard.

Ahd who broke the code formatter?!


1 - In C, that is; in C++, the cast is required, but if you're writing C++, you should be using new instead of malloc anyway.

Upvotes: 0

Mahesh
Mahesh

Reputation: 34625

You can not call assignment operations at global scope. Try malloc operation in main() instead.

And the type of stack is not a pointer but pointer to pointer. Are you sure about it's declaration ?

Upvotes: 2

Related Questions