Array or Structs? and using sscanf

This is a discussion on Array or Structs? and using sscanf within the C Programming forums, part of the General Programming Boards category; I have two questions: 1. Can I have an array of structs and if so, how do I define it? ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    14

    Array or Structs? and using sscanf

    I have two questions:

    1. Can I have an array of structs and if so, how do I define it? I know how to define a struct, but not an array of structs if its possible.

    2. If I have a csv string containing 2 ints and a string. how can I separate them using sscanf? An example format of the string is "0,5,TEST" I was reading on sscanf and saw that if there is a space, it will only take the value up to the space so do i need to make the string "0 5 TEST" instead of using csv and do 3 separate sscanf using %d %d and then again using %s???

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. Of course. You put [how_many_do_you_want] after the name, just like every other array.

    2. Your format should match the data. If your data is number,number,word then that's what your format should be: "%d,%d,%s". (Now if your word can have a space in it then we'd have to deal with that.)

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    1. Thanks!

    2. So since there are commas separating the values, sscanf will recognize the commas in the format string and not include them in the data?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Yup. The format string is a template of how the input is scanned. There may be fancier format parameters available such as “regular expressions” if you wish to allow multiple separator characters. But for simple commas, this will do.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21