File -> structure

This is a discussion on File -> structure within the C Programming forums, part of the General Programming Boards category; I have a problem assigning value to a variable that is in a structure. I think that's the row with ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    16

    File -> structure

    I have a problem assigning value to a variable that is in a structure.

    I think that's the row with the problem:
    Code:
    fscanf(fp, "%s %d\n", h->name[c_names], h->age[c_names]);
    Is this the rights way to assign string and int? &h->age[c_names] also doesn't work.

    Also is r->price++; correct?
    Last edited by Etdim; 12-20-2007 at 01:48 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    I'm thinking it should look like:
    Code:
    fscanf(fp, "%s %d\n", &h->name[c_names], &h->age[c_names]);
    But I don't know since I don't know what types they are - how about showing your struct?
    Also read this since you're using fscanf("%s", ...).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    16
    Code:
    struct person{
            char name[20][30];
            int age[20];
            };
    I'm passing the pointer *h to the function and I want the read data to be stored in the structure. I've done typedef and I've defined "h".

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Then it should be:
    Code:
    fscanf(fp, "%s %d\n", h->name[c_names], &h->age[c_names]);
    Since name is a 2D-array, it will basically be a char**, so name[c_names] will be char*, so no & should be required.
    But the age needs &, because age[c_names] will be int.

    I assume price is of type int or some such inside a struct (which you dereference with ->), if so, then yes, that is correct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I always forget the precedence rules, so I practice defensive parenthesizing: &(h->age[cnames]). That should work. Of course, fscanf with %s isn't any good anyway.

    As to r->price++, that will add 1 (or 1.0, or 1.0f) to the value of r->price. So provided r is a pointer, and price is a scalar (not an array) in the struct pointed to by r, everybody's happy.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Quote Originally Posted by tabstop View Post
    I always forget the precedence rules, so I practice defensive parenthesizing: &(h->age[cnames]). That should work.
    h->age[c_names] should do fine.

    Of course, fscanf with %s isn't any good anyway.
    I do so agree.
    Etdim: I hope you read that link I gave you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading structure from file..??
    By CyC|OpS in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 05:28 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 10:47 AM

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