Thread: Help for my code

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    21

    Help for my code

    Hello everyone,
    I'm new entry here, my name is Jacopo, from Italy.
    I'm trying study c language and I have write a code to make a list but now I need some help for this.
    So, this is not exactly my code, this is only a test code that I did to check memory address of list elements

    Code:
    #include <stdio.h>
    
    
    struct Element {
        int info;
        struct Element *next;
        struct Element *prev;
    };
    
    
    struct Element New_Element(int info);
    
    
    int main(int argc, char **argv)
    {
        int a, c;
        printf("How many elements do you want put in? ");
        scanf("%d", &c);
        for (int h = 0; h < c; h++) {
            printf("Insert a: ");
            scanf("%d", &a);
            struct Element e = New_Element(a);
            printf("Elemento%d struct address: %p \n\n", h, &e);
        }
        return 0;
    }
    
    struct Element New_Element(int info) {
        struct Element e = {info, NULL, NULL};
        return e; 
    }
    My issue is that the compiler assign every time the same memory location to each elements (see image below). This isn't good to me because I need to have a pointers that point different elements and therefore different memory address.
    How can I do this?
    Ps. My english is not perfect, sorry

    Help for my code-capture-png
    Last edited by Jacopo; 02-16-2018 at 05:28 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    You need to call malloc if you want a dynamic number of elements to exist in your program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    21
    Thanks for reply me.
    I'm able to use malloc function to make dynamic array, but how can I use malloc to initialize struct?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,926
    Quote Originally Posted by Jacopo
    I'm able to use malloc function to make dynamic array, but how can I use malloc to initialize struct?
    You can't, but then once you have your dynamic array of struct objects thanks to malloc, you can loop over the array and assign initial values to the members of each struct object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2018
    Posts
    21
    mh, can you show me an example?

    but then once you have your dynamic array of struct objects thanks to malloc
    How?
    Last edited by Jacopo; 02-16-2018 at 08:52 AM.

  6. #6
    Registered User
    Join Date
    Feb 2018
    Posts
    21
    Ok, I have tried to rewrite my code with the malloc function but the result not change..Where am I doing wrong?
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    struct Element {
        int info;
        struct Element *next;
        struct Element *prev;
    };
    
    
    struct Element *New_Element(int info);
    
    
    int main(int argc, char **argv)
    {
        int a, c;
        printf("How many elements do you want put in? ");
        scanf("%d", &c);
        for (int h = 0; h < c; h++) {
            printf("Insert a: ");
            scanf("%d", &a);
            struct Element e = *New_Element(a);
            printf("Elemento%d struct address: %p \n\n", h, &e);
        }
    	return 0;
    }
    
    
    struct Element *New_Element(int info) {
        struct Element *e = malloc(sizeof(struct Element));
        (*e).info = info;
        (*e).next = NULL;
        (*e).prev = NULL;
        return e; 
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    You should print e, not &e.

    struct Element *e = New_Element(a);
    printf("Elemento%d struct address: %p \n\n", h, e
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Feb 2018
    Posts
    21
    Quote Originally Posted by Salem View Post
    You should print e, not &e.

    struct Element *e = New_Element(a);
    printf("Elemento%d struct address: %p \n\n", h, e
    Yeah I was writing this right now!
    Thanks to all!

    So, this code works but my last doubt...is this the best way to initialize an struct (or this type of struct)?

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    struct Element {
        int info;
        struct Element *next;
        struct Element *prev;
    };
    
    
    struct Element *New_Element(int info);
    
    
    int main(int argc, char **argv)
    {
        int a, c;
        printf("How many elements do you want put in? ");
        scanf("%d", &c);
        for (int h = 0; h < c; h++) {
            printf("Insert a: ");
            scanf("%d", &a);
            struct Element *e = New_Element(a);
            printf("Elemento%d struct address: %p \n\n", h, e);
        }
        return 0;
    }
    
    
    struct Element *New_Element(int info) {
        struct Element *e = malloc(sizeof(struct Element));
        (*e).info = info;
        (*e).next = NULL;
        (*e).prev = NULL;
        return e; 
    }
    Last edited by Jacopo; 02-16-2018 at 09:54 AM.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Looks good, but a few things you can do to clean it up:

    • malloc can return NULL if it fails (see man page), so you should check for that. If you don't and it returns NULL, you will end up trying to access memory you don't own, which causes undefined behavior.
    • I generally use the variable itself (dereferenced) in the sizeof. That's one less thing to change if for any reason you rename the struct Element to something else.
    • I prefer the -> notation to (*). for struct member access; it seems cleaner.


    Taking those into account, your function might look something like:
    Code:
    struct Element *New_Element(int info) {
        struct Element *e = malloc(sizeof(*e));
        if (e != NULL) {
            e->info = info;
            e->next = NULL;
            e->prev = NULL;
        }
        return e; 
    }
    Last edited by anduril462; 02-16-2018 at 11:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

Tags for this Thread