开发者

whats wrong in this program?

开发者 https://www.devze.com 2023-03-10 21:55 出处:网络
Am unable to run this code... #include<cstdio> int main() { struct a{ int b; struct a *nex开发者_运维知识库t;

Am unable to run this code...

#include<cstdio>
int main()
{
struct a{
    int b;
    struct a *nex开发者_运维知识库t;
};

    typedef struct a no;

    no *n;

    n->b = 12;
    n->next = NULL;

    n->next->b = 12;
    n->next->next = NULL;

    printf("%d %d", n->b, n->next->b);
    getchar();
    return 0;
}


When you say:

no *n;

you get an uninitialised pointer. When you use that pointer, you get undefined behaviour.


You allocated space for a pointer to a structure, but you didn't allocate space for the actual structure. This means that you don't have a memory address for the structure you are using.

In addition, the pointer points to some random memory address because you didn't initialize it. As a result, you could be trying to read and write to memory that doesn't belong to you, which can cause your program or even your system to crash because of the undefined behavior that results.


As @Neil Butterworth said, you get an uninitialised pointer. This mean that this pointer could point to anywhere, thus giving an access violation error. The way to fix this is simple, just call malloc() before using that pointer. malloc() gives that pointer a valid and usable address, so no one will complain about that.


You're declaring a struct INSIDE a function.

Declare the struct OUTSIDE of the function.

The typedef should be declared outside the function too.

#include<cstdio>

struct a{
    int b;
    struct a *next;
};

    typedef struct a no;

int main()
{

 ///... your code...
}


try something like this:

no *n = (no*)malloc(sizeof(no));


#include <cstdio>

/* declaring the struct in the function definition may be possible (I'm not sure,
   actually, haha).  Unless you have a GOOD reason, it's good practice to declare
   structs, globals, typedefs, etc... outside the function */

typedef struct a{
    int b;
    struct a *next;
} no;

int main()
{
    no *n;

    /* Here, you have a pointer.  Remember these are simply (generally) 32-bit values
       defined in your stack space used to store a memory location which points to your
       ACTUAL struct a!  Depending on what USED to be in the stack space, this could
       point ANYWHERE in memory, but usually you will find that it points to the NULL
       memory location, which is just address "0".  To get this to point to something,
       you have to allocate empty space on your heap to store your struct... */

    n = malloc(sizeof(no));

    /* Now your pointer n points to an allocated 'struct a', and you can use it like
       normal */

    n->b = 12;
    n->next = NULL;

    /* You just set n->next, which is another 'no' pointer, to NULL.  This means that
       n->next points nowhere.  So, just like above you have to malloc another instance
       of the struct! */

    n->next = malloc(sizeof(no));

    /* NOW you can use n->next with no ill effects! */

    n->next->b = 12;
    n->next->next = NULL;

    printf("%d %d", n->b, n->next->b);
    getchar();

    /* After you're done with your structs, you want to free them using the POINTERS
       that reference them */

    free(n->next);
    free(n);

    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消