Thread: Passing a struct into array.

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    Passing a struct into array.

    This post is related to the previous post in the content, but the subject is totally here different..
    this time, I want to save the input's struct details into one place to an array that's called "sizeofdocument", and its size 100 (we actually imagine it like a memory's saving).
    Moreover, every place in the sizeofdocument has its certain struct's inputs, for instances:
    we got those inputs:
    struct's input=('bob','Nike',1,"Yeschannel")
    struct's input=('bob','Nike',2,"Yeschannel")
    struct's input=('bob','Nike',3,"Yeschannel")
    the resultthe struts must be orderly saved from index 0 to....)
    like (the first struct's input, the second's struct input,the third struct's input,..................................), so as we see in the first index 0 in the sizeofdocument, the first struct's input entered there..etc.

    I've done with this code, but I really don't know why isn't working, I tried more than once:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include<stdlib.h>
    #define sizeofdocument (100)
    typedef struct
    {
        char name[4];
        char type[4];
        int  size;
        char *addressInRam;
    }memIdentifier;
    
    
     
    main()
    {
        memIdentifier *mapArray[sizeofdocument];
        memIdentifier *mapArray[sizeofdocument]={'bob','Nike',1,"Yeschannel"}
        memIdentifier *mapArray[sizeofdocument]={'bob','Pullo',1,"clinicchannel"}
        printf("%d", *mapArray[sizeofdocument]);
        printf("%s", *mapArray[sizeofdocument]);
        return 0;
    }
    Any help please?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why is mapArray an array of pointers to memIdentifier objects instead of an array of memIdentifier objects?

    By the way, instead of this:
    Code:
    #define sizeofdocument (100)
    I suggest:
    Code:
    #define SIZE_OF_DOCUMENT 100
    It is a common convention to use fully uppercase names for macros, with the underscores used to improve readability. The parentheses around 100 are optional here since 100 is not a complex expression in which precedence might come into play.
    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

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Why is mapArray an array of pointers to memIdentifier objects instead of an array of memIdentifier objects?

    By the way, instead of this:
    Code:
    #define sizeofdocument (100)
    I suggest:
    Code:
    #define SIZE_OF_DOCUMENT 100
    It is a common convention to use fully uppercase names for macros, with the underscores used to improve readability. The parentheses around 100 are optional here since 100 is not a complex expression in which precedence might come into play.

    I used what you ordered me, but still getting the same wrong result, may you gimme a hint? even not specifically to this example, make it in generally please.

  4. #4
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Why is mapArray an array of pointers to memIdentifier objects instead of an array of memIdentifier objects?

    By the way, instead of this:
    Code:
    #define sizeofdocument (100)
    I suggest:
    Code:
    #define SIZE_OF_DOCUMENT 100
    It is a common convention to use fully uppercase names for macros, with the underscores used to improve readability. The parentheses around 100 are optional here since 100 is not a complex expression in which precedence might come into play.
    I can by an array and that would be easy, I'm curious and struggling to know how to deal with it with an pointer's array.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    I used what you ordered me, but still getting the same wrong result
    My comment on the macro was "by the way". It is an issue of style that does not affect the correctness of your code in itself, so taking my suggestion would certainly not fix your code.

    Quote Originally Posted by RyanC
    I can by an array and that would be easy, I'm curious and struggling to know how to deal with it with an pointer's array.
    Ah, so you used an array of pointers to memIdentifier objects not because you thought that it was the best choice, but for the sake of practice? That's okay, but first: write and post the program such that it uses an array of memIdentifier objects. From there, we can help you modify it to use an array of pointers to memIdentifier objects.
    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

  6. #6
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    My comment on the macro was "by the way". It is an issue of style that does not affect the correctness of your code in itself, so taking my suggestion would certainly not fix your code.


    Ah, so you used an array of pointers to memIdentifier objects not because you thought that it was the best choice, but for the sake of practice? That's okay, but first: write and post the program such that it uses an array of memIdentifier objects. From there, we can help you modify it to use an array of pointers to memIdentifier objects.
    You are talking on touch, and here what I've got(btw-your hints and how you react is just really pull up my skills )
    I thought it's easier without a pointer's array without actually trying..just supposed that!!
    here's the code without a pointer:
    lets say I want to enter a book of information(talking on the first's struct inputs)into the first place of my array.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include<stdlib.h>
    #define SIZE_OF_Document (100)
    typedef struct
    {
        char name[4];
        char type[4];
        int  size;
        char *addressInRam;
    }memIdentifier;
    
    
     
    main()
    {
        
        memIdentifier mapArray[SIZE_OF_Document (100)];
        memIdentifier mapArray[0]={'bob','Pull',1,"clini"};
        printf("%d",mapArray.[0]);
        return 0;
    }
    I know there's ofcourse errors, but it's actually the first time I'm dealing with array relevantly to a struct, so any help please?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let's start with the macro. As I noted, the convention is fully uppercase names, so this:
    Code:
    #define SIZE_OF_Document (100)
    should be:
    Code:
    #define SIZE_OF_DOCUMENT 100
    Next, you had the right syntax in your post #1, but now your use of the macro is wrong here:
    Code:
    memIdentifier mapArray[SIZE_OF_Document (100)];
    It should have been:
    Code:
    memIdentifier mapArray[SIZE_OF_DOCUMENT];
    This is wrong:
    Code:
    memIdentifier mapArray[0]={'bob','Pull',1,"clini"};
    The problem is that you can initialise arrays, but you cannot assign to them. The above code looks like a mix of an attempt to redeclare mapArray and an attempt to assign to mapArray[0]. Rather, you should initialise mapArray. Furthermore, you should be using string literals, i.e., be careful of single quotes versus double quotes. So, you should have removed this line and written:
    Code:
    memIdentifier mapArray[SIZE_OF_DOCUMENT] = {{"bob", "Pull", 1, "clini"}};
    Now, look at this line:
    Code:
    printf("%d",mapArray.[0]);
    What are you trying to do in the above line? It looks like you want to print an int because of the %d, but mapArray.[0] is invalid syntax.

    By the way, this is poor style:
    Code:
    main()
    You should write:
    Code:
    int main(void)
    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

  8. #8
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Let's start with the macro. As I noted, the convention is fully uppercase names, so this:
    Now, look at this line:
    Code:
    printf("%d",mapArray.[0]);
    What are you trying to do in the above line? It looks like you want to print an int because of the %d, but mapArray.[0] is invalid syntax
    [/code]
    Well, I want to do printf for checking if it's saving in it or not!, just put a "printf" for assuring it's saving or not in the maparray.
    btw, what should I type instead of "%d" for letting the printf work perfectly?


    At the end, your tips are very useful and are perfectly for new-programmers.

  9. #9
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Let's start with the macro. As I noted, the convention is fully uppercase names, so this:

    Code:
    memIdentifier mapArray[0]={'bob','Pull',1,"clini"};
    The problem is that you can initialise arrays, but you cannot assign to them. The above code looks like a mix of an attempt to redeclare mapArray and an attempt to assign to mapArray[0]. Rather, you should initialise mapArray. Furthermore, you should be using string literals, i.e., be careful of single quotes versus double quotes. So, you should have removed this line and written:
    Code:
    memIdentifier mapArray[SIZE_OF_DOCUMENT] = {{"bob", "Pull", 1, "clini"}};
    So is it automatically gets to the first place in the maparray? meaning in maparray[0]?
    I'm trying to do something like, if you put a struct's input, then as a memory..save it in one byte in an array, and if gets another...the same thing but in the next place...etc

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    Well, I want to do printf for checking if it's saving in it or not!, just put a "printf" for assuring it's saving or not in the maparray.
    That's fine, but you need to use valid syntax. Since mapArray is an array of memIdentifier objects, mapArray[0] is the first memIdentifier object. Hence, if you want to access say, the size member, you would write mapArray[0].size.

    Quote Originally Posted by RyanC
    btw, what should I type instead of "%d" for letting the printf work perfectly?
    The %d is fine for printing the size member. To print an entire memIdentifier object, you need to come up with some output format, then implement it. Because you will likely want to print it more than once, it is advisable to implement a function, e.g.,
    Code:
    void memIdentifier_print(FILE *fp, const memIdentifier *obj)
    {
        fprintf(fp, "%s %s %d", obj->name, obj->type, obj->size);
    }
    Then you can call the function, e.g.,
    Code:
    memIdentifier_print(stdout, &mapArray[0]);
    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

  11. #11
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    That's fine, but you need to use valid syntax. Since mapArray is an array of memIdentifier objects, mapArray[0] is the first memIdentifier object. Hence, if you want to access say, the size member, you would write mapArray[0].size.


    The %d is fine for printing the size member. To print an entire memIdentifier object, you need to come up with some output format, then implement it. Because you will likely want to print it more than once, it is advisable to implement a function, e.g.,
    Code:
    void memIdentifier_print(FILE *fp, const memIdentifier *obj)
    {
        fprintf(fp, "%s %s %d", obj->name, obj->type, obj->size);
    }
    Then you can call the function, e.g.,
    Code:
    memIdentifier_print(stdout, &mapArray[0]);
    Alright, lets say I wrote twice this code:
    Code:
    memIdentifier mapArray[SIZE_OF_DOCUMENT] = {{"bob", "Pull", 1, "clini"}};//(1)
    memIdentifier mapArray[SIZE_OF_DOCUMENT] = {{"bb", "ull", 1, "clini"}};//(2)
    will I get in the maparry [0] the first input of struct, meaning (1)...?and in the next place maparray[1]..same thing? but the other input (2).

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, you would get an error. You need to recognise that this:
    Code:
    memIdentifier mapArray[SIZE_OF_DOCUMENT] = {{"bob", "Pull", 1, "clini"}};
    is a declaration of mapArray that is also a definition of mapArray that initialises it. You can only define an object once. Therefore, if you want to initialise the array with two elements specified in the initialiser, you should write the initialiser to have those two elements.
    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

  13. #13
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    No, you would get an error. You need to recognise that this:
    Code:
    memIdentifier mapArray[SIZE_OF_DOCUMENT] = {{"bob", "Pull", 1, "clini"}};
    is a declaration of mapArray that is also a definition of mapArray that initialises it. You can only define an object once. Therefore, if you want to initialise the array with two elements specified in the initialiser, you should write the initialiser to have those two elements.
    Sorry for that, but may you write ur speech by a code? would be more understandable..thanks.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    Sorry for that, but may you write ur speech by a code? would be more understandable..thanks.
    Well, suppose you have an array of 5 integers and want to initialise it with two elements. You might write:
    Code:
    int numbers[5] = {123, 456};
    You would not write:
    Code:
    int numbers[5] = {123};
    int numbers[5] = {456};
    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

  15. #15
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Well, suppose you have an array of 5 integers and want to initialise it with two elements. You might write:
    Code:
    int numbers[5] = {123, 456};
    You would not write:
    Code:
    int numbers[5] = {123};
    int numbers[5] = {456};
    Excellent!! and succeeded, additionally to what I demanded, I would like to add since you have an array with the given inputs...in my case lets say I have in
    Code:
    maparray[0]={{"bob", "Pull", 1, "clini"}};
    , and in
    Code:
    maparray[1]={{"bob", "Pull", 1, "clini"}};
    , so I can in index 0 there's a certain input, and in the next index of an array there's also a specific input, I'm willing now to do this condition:
    if you write the name of the struct(by a scanf-from the user himself)-int this case- I supposed it the first element in
    Code:
    maparray[0]={{"bob", "Pull", 1, "clini"}};
    , for instance, here the name of the struct is "bob"..etc, so if you write "bob" in scanf, then you must go through all the elements of the given's struct name- and print all the elements.
    To sump up, here is an example of:
    a user wrote "bob", then we search through the maparray for any array that's bear this name-actually in my case once again I supposed the name of the struct is appearing always in the first elements of
    Code:
    {}
    , and print all its elements-"pull",1,"clini". that's all!

    Any help? I dont know actually how to do a while loop inside the maparray because it has also in everyplace another
    Code:
    {}
    includes elements!

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Struct through Array
    By emverhov in forum C Programming
    Replies: 1
    Last Post: 04-02-2013, 10:18 AM
  2. Replies: 8
    Last Post: 05-10-2012, 12:07 PM
  3. Replies: 1
    Last Post: 05-30-2009, 11:04 PM
  4. Passing array of struct as a function parameter
    By desmond5 in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 11:32 AM
  5. passing array of type struct to function
    By nappaji in forum C Programming
    Replies: 4
    Last Post: 05-02-2007, 05:13 PM