struct problem and general C question

This is a discussion on struct problem and general C question within the C Programming forums, part of the General Programming Boards category; First question, whats wrong with this stucture: Code: #include "edit.h" struct file_info { int buffer[BUFSIZE]; int position; char charAt; } ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    35

    struct problem and general C question

    First question, whats wrong with this stucture:
    Code:
    #include "edit.h"
    
    struct file_info {
      int buffer[BUFSIZE];
      int position;
      char charAt;
    } *file;
    
    int displayFile(FILE *fp)
    {
      file = malloc(sizeof * file);
    
      return 0;
    }
    It says there needs to be a semicolon somewhere, but I have that.
    My second question: In c, is there a place within a function before or after something that you cannot declare variables? I tried declaring an int right after a malloc statement above it, and it was giving me undefined indentifier problems. Are there any general rules about declaration that I should know/are different than in c++? Thanks a lot.
    you make me rery ascared

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    if you mean [BUFSIZ] defined in stdio.h then it doesn't have a E like in your code unless you defined it.also
    Code:
    file = malloc(sizeof * file);
    should be
    Code:
    file=malloc(sizeof(struct file_info));

  3. #3
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    Code:
    file = malloc(sizeof *file);
    
    file = malloc(sizeof(struct file_info));
    both are correct

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    3
    Originally posted by viaxd
    Code:
    file = malloc(sizeof *file);
    
    file = malloc(sizeof(struct file_info));
    both are correct
    You don't have to put parenthesis around the *file?

  5. #5
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    You don't have to put parenthesis around the *file?
    no you don't

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    no, I have BUFSIZE defined in edit.h. I was wondering about basic c declaration requierments, such as, is it legal to define a variable after something like this:
    Code:
     int a;
     int charty[80];
    charty = gets(charty);
    int g;  //this didn't seem to compile.
    and other things that are/are not allowed. Thanks.
    you make me rery ascared

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    > int g; //this didn't seem to compile.
    That's because it appeared after a statement, and C doesn't (yet) allow declarations anywhere in the code (unlike say C++)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    so if I have a statement, the declarations MUST come before it? Is there a good solid book to learn all this from? Any recommendations?
    Last edited by techrolla; 01-08-2004 at 06:16 PM.
    you make me rery ascared

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    > Are there any general rules about declaration that I should know/are different than in c++?
    http://david.tribble.com/text/cdiffs.htm
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. General linking question
    By BrandonW in forum C Programming
    Replies: 5
    Last Post: 05-02-2007, 12:24 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Template problem question
    By grscot in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2003, 12:31 PM
  5. general question regarding a function parameter
    By mlupo in forum C Programming
    Replies: 7
    Last Post: 10-13-2002, 07:32 PM

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