Filling char field in struct and array of a struct

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • odin607
    New Member
    • Jun 2008
    • 19

    Filling char field in struct and array of a struct

    Question 1: How can i get Name filled with the name steve without having to scan or fscan using C?

    Code:
    typedef struct
       {
    	char  name[40];
       } PERSON;
    Question 2: How would I make an array (lets say of size 5) with each element being type PERSON. Once made how would I print out the name in element 3?
  • jorba101
    New Member
    • Jun 2008
    • 86

    #2
    If input comes from a stream (STDIN, file ... ) you can always fill it in with syscall read(), which is what actually scanf does.

    See http://www.opengroup.o rg/onlinepubs/007908799/xsh/read.html

    If the assignment is to be hardcoded, then you can use strcpy from string.h or directly assigning the chars, i.e.

    Code:
    PERSON personVar;
    
    personVar.name[0]='S';
    personVar.name[1]='t";
    ...
    personVar.name[5] ='\0';

    Comment

    • jorba101
      New Member
      • Jun 2008
      • 86

      #3
      Your second question pretty much sound like your homework.

      Comment

      • odin607
        New Member
        • Jun 2008
        • 19

        #4
        nah its not homework (its summer duh) i figured someone would say that though so heres the story behind it if you really wanna know~

        both of these questions are from the same place...i realized i didnt know how to do either of them when i was helping a friend towards the end of the year with his first semester C class (im on my second, hence why i was like why dont i know this?)

        but anyways so theres really no way to do "steve" directly then eh =/ (towards Q1)

        and any answer on Q2? you can wait a week to answer if you really think its homework still doesnt bother me at all.

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          but anyways so theres really no way to do "steve" directly then
          Your question is confusing. What do you mean “directly”? If you don’t want to use formatted input (which is what the scanf family is) then your alternative is fgets (which is usually what you want to use anyway). If you seek to read input without using the standard library, then you’ll have to go through the OS specific APIs.

          and any answer on Q2?
          Consider for a moment, the question, of how would you make an array of size 5, of element type char. Clearly, it’s
          Code:
          char something[5]
          Now look at the semantics of the structure definition you have there. You define a structure, with a member name. This structure defines a new datatype, and you give this datatype a new name (with typedef): PERSON. Now, swap out the element type char with element with PERSON. So you have:
          Code:
          PERSON something[5]
          Once made how would I print out the name in element 3?
          Printing out can be done with puts or printf. You want to pass in the array name, so you have something[2].name for the printf or puts argument.

          Comment

          • odin607
            New Member
            • Jun 2008
            • 19

            #6
            hmm sorry for being vague there

            since its kinda strange to explain ill give an example

            same idea except with int (since obviously this works)

            int user_id;
            user_id=4;

            is it possible to do something like this where the program inputs the value itself (in the example where it sets it = to 4).

            and thanks on Q2, i got it to work.. i knew conceptually that it would be something like that, but it just didnt click till i read that

            Comment

            • oler1s
              Recognized Expert Contributor
              • Aug 2007
              • 671

              #7
              Oh, so you want to do something like
              Code:
              name = “Steve”
              That won’t work. C strings aren’t true datatypes. They’re implemented as character arrays, so you have to treat them as arrays. If you have a C string and you want to put something like “Steve” in it, your best bet is to rely on strcpy. Anything else is going to be inefficient.

              Comment

              • odin607
                New Member
                • Jun 2008
                • 19

                #8
                ya thanks strcpy is what i was looking for... forgot about it till i ran into it after like 3 hours of googling stuff

                anywho thank yee again

                Comment

                Working...