Like Tree3Likes
  • 2 Post By memcpy
  • 1 Post By Salem

Accessing Structs in Functions Problem

This is a discussion on Accessing Structs in Functions Problem within the C Programming forums, part of the General Programming Boards category; This C program is supposed to ask the user for the number of rows and columns, and output a rectangle ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    7

    Unhappy Accessing Structs in Functions Problem

    This C program is supposed to ask the user for the number of rows and columns, and output a rectangle of asterisks, like a visual times table. Not even getting to the output part yet, structs confuse the hell out of me, and when i try to compile, gcc gives these errors:

    multiply.c:18: error: expected expression before box
    multiply.c: In function draw_box:
    multiply.c:51: error: incompatible type for argument 1 of draw_line
    multiply.c:11: note: expected box but argument is of type int

    really, using struct variables with functions is what's throwing me off

    Code:
    #include <stdio.h>
    
    typedef struct {
    int rows;
    int columns;
    } box;
    
    int input_rows();
    int input_columns();
    void draw_box();
    void draw_line(box data);
    
    int main()
    {
    box data;
    data.rows = input_rows();
    data.columns = input_columns();
    draw_box(box data);
    
    return 0;
    }
    
    int input_rows(){
    
    box data;
    
    do{
    printf ("Number of rows? \n");
    scanf ("%d", &data.rows);
    } while (data.rows <= 0);
    return (data.rows);
    }
    
    int input_columns(){
    
    box data;
    
    do{
    printf ("Number of columns? \n");
    scanf ("%d", &data.columns);
    } while (data.columns <= 0);
    return (data.columns);
    }
    
    void draw_box(){
    
    box data;
    int x;
    for(x=1;x<=data.rows;x++)
    {
    draw_line(data.columns);
    }
    }
    
    void draw_line(box data){
    
    int y;
    for(y=1;y<=data.columns;y++);
    {
    printf("*");
    }
    }
    Last edited by Salem; 04-02-2012 at 09:11 PM. Reason: restored

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You are calling it as draw_line(data.columns). data.columns is an int not a box, therefore the compiler is just noticing the obvious.

    Also your draw_box function takes no arguments, yet you are passing it your box from main. You are then declaring a bogus local box in draw_box and are attempting to iterate to God knows where.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Code:
    #include <stdio.h>
    
    typedef struct {
      int rows;
      int columns;
    } box;
    
    int input_rows();
    int input_columns();
    void draw_box(); //!! you forgot to add the box parameter
    void draw_line(box data);
    
    int main()
    {
      box data;
      data.rows = input_rows();
      data.columns = input_columns();
      draw_box(box data);  //!! delete the type, functions are called with variable names only
    
      return 0;
    }
    
    int input_rows(){
    
      box data;  // All you're interested in is an int, not a whole box
    
      do{
           printf ("Number of rows? \n");
           scanf ("%d", &data.rows);
       } while (data.rows <= 0);
      return (data.rows);
    }
    
    int input_columns(){
    
      box data; //!! ditto
    
      do{
           printf ("Number of columns? \n");
           scanf ("%d", &data.columns);
       }  while (data.columns <= 0);
      return (data.columns);
    }
    
    void draw_box(){
    
      box data;  //!! this box contains junk - it would be better with a parameter
      int x;
      for(x=1;x<=data.rows;x++)
      {
          draw_line(data.columns);
        }
    }
    
    void draw_line(box data){
    
      int y;
      for(y=1;y<=data.columns;y++);
      {
        printf("*");
      }
    }
    See the !! comments.
    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.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Looks, like the magic eye of Salem compiled it for you . Didn't notice all the other mistakes (actually the same mistake over and over).
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    if i change 'box' in those places to 'int', i get all kinds of 'request for member in something not a structure or union' errors

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Well yes, you need
    int var;
    scanf("%d",&var);
    return var;
    kinda thing.
    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.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    thank you

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Don't delete threads after you get help. This defeats the point of being a public forum of which others can learn from.
    Salem and phantomotap like this.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    post fixed (and thread close)
    phantomotap likes this.
    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. Problem with structs and functions
    By Moose112 in forum C++ Programming
    Replies: 11
    Last Post: 12-01-2006, 10:04 PM
  2. accessing variables using structs
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 04-25-2006, 11:09 AM
  3. accessing members in nested structs
    By amidstTheAshes in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 01:00 PM
  4. accessing array of structs values
    By WaterNut in forum C++ Programming
    Replies: 12
    Last Post: 07-08-2004, 08:47 PM
  5. Problem displaying (structs functions)
    By dayknight in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2004, 05:11 PM

Tags for this Thread


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