Global variables sent to a function

This is a discussion on Global variables sent to a function within the C Programming forums, part of the General Programming Boards category; Ok I wanna create a program that displays some numbers. I don't wanna use pointers for a reason but its ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    Global variables sent to a function

    Ok I wanna create a program that displays some numbers. I don't wanna use pointers for a reason but its kinda complicated. Anyway heres my code can anyone tell me a way to make it work?

    Working version(modified just to work with one struct):

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    typedef struct
    {
    int x[3];
    int y;
    int total;
    }Data;
    
    void Display(Data tempx);
    
    Data numbers;
    
    int main()
    {
    numbers.total=3;
    numbers.y=0;
    numbers.x[0]=5;
    numbers.x[1]=3;
    numbers.x[2]=1;
    while(1)
    Display(numbers);	
    
    getch();	
    }
    
    void Display(Data tempx)
    {
    if(numbers.y>=numbers.total)
    {
    numbers.y=0;
    }
    else{
    printf("%d \n", numbers.x[numbers.y]);
    numbers.y++;
    getch();
    }
    }
    Not Working:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    typedef struct
    {
    int x[3];
    int y;
    int total;
    }Data;
    
    void Display(Data tempx);
    
    Data ;
    
    int main()
    {
    numbers.total=3;
    numbers.y=0;
    numbers.x[0]=5;
    numbers.x[1]=3;
    numbers.x[2]=1;
    while(1)
    Display(numbers);	
    
    getch();	
    }
    
    void Display(Data tempx)
    {
    if(tempx.y>=tempx.total)
    {
    tempx.y=0;
    }
    else{
    printf("%d \n", tempx.x[tempx.y]);
    tempx.y++;
    getch();
    }
    }
    Can anyone make the non working thing to work so that I can also send other structs to Display. I would prefer not to use pointers if possible.

    Also is there anyway to maybe pass by reference instead of using pointers like in C++? Maybe someone knows a library someone came up with to make it work. If you don't know what I mean. I'm talking about something like this:

    void Display(Data &tempx);

    So that you can call upon it like this:

    Display(numbers);

  2. #2
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    You never declared numbers to be of the data type.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  3. #3
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by ExtremelyStupid
    Ok I wanna create a program that displays some numbers. I don't wanna use pointers for a reason
    What reason? Pointers are the essence of the C-language. If you don't like to see them, use another language.
    but its kinda complicated. Anyway heres my code can anyone tell me a way to make it work?

    Working version(modified just to work with one struct):
    What do you mean 'to work'. The parameter 'tempx' is here, but it is not used at all.
    Not Working:
    (Reformatted, fixed and commeted)
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    typedef struct
    {
       int x[3];
       int y;
       int total;
    }
    Data;
    
    void Display (Data tempx);
    
    /* -ed-
    Data; 
     * Wrong. What is this supposed to be ? 
     * Removed.
     */
    
    int main ()
    {
    /* -ed- Missing definition of 'numbers' */
       Data numbers; 
    
       numbers.total = 3;
       numbers.y = 0;
       numbers.x[0] = 5;
       numbers.x[1] = 3;
       numbers.x[2] = 1;
       while (1)
          Display (numbers);
    
       getch ();
    }
    
    void Display (Data tempx)
    {
       if (tempx.y >= tempx.total)
       {
          tempx.y = 0;
       }
       else
       {
          printf ("%d \n", tempx.x[tempx.y]);
          tempx.y++;
          getch ();
       }
    }
    Can anyone make the non working thing to work so that I can also send other structs to Display. I would prefer not to use pointers if possible.
    Fixed. While this code is technically correct, it's considered bad practice (and it is very unefficient) to pass a copy of a structure to a functions. Pointers avoid that.

    Additionally, the behaviour of the code is not the one you expect (I guess) because you want to alter some data. In that case, the pointer is not an option.
    Also is there anyway to maybe pass by reference instead of using pointers like in C++?
    No way. If you want C++, you know what to do.

    Also, don't write infinite loops in your testing code.

    I suggest the following:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
       int x[3];
       int y;
       int total;
    }
    Data;
    
    void Display (Data *this)
    {
       if (this->y >= this->total)
       {
          this->y = 0;
       }
       else
       {
          printf ("%d \n", this->x[this->y]);
          this->y++;
       }
    }
    
    int main (void)
    {
       Data numbers; 
    
       numbers.total = 3;
       numbers.y = 0;
       numbers.x[0] = 5;
       numbers.x[1] = 3;
       numbers.x[2] = 1;
       
       {
          int i;
          for (i = 0; i < 10; i++)
          {
             Display (&numbers);
          }
       }
       system ("pause");
       
       return 0;
    }
    That produces:
    Code:
    5
    3
    1
    5
    3
    1
    5
    3
    Appuyez sur une touche pour continuer . . .
    Emmanuel Delahaye

    "C is a sharp tool"

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    Err typo. I fixed it in my original program still same result. It was suppose to be Data numbers;
    Last edited by ExtremelyStupid; 08-02-2004 at 03:55 AM.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    Quote Originally Posted by Emmanuel Delaha
    What reason? Pointers are the essence of the C-language. If you don't like to see them, use another language.

    What do you mean 'to work'. The parameter 'tempx' is here, but it is not used at all.

    (Reformatted, fixed and commeted)

    Fixed. While this code is technically correct, it's considered bad practice (and it is very unefficient) to pass a copy of a structure to a functions. Pointers avoid that.

    Additionally, the behaviour of the code is not the one you expect (I guess) because you want to alter some data. In that case, the pointer is not an option.

    No way. If you want C++, you know what to do.

    Also, don't write infinite loops in your testing code.

    I suggest the following:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
       int x[3];
       int y;
       int total;
    }
    Data;
    
    void Display (Data *this)
    {
       if (this->y >= this->total)
       {
          this->y = 0;
       }
       else
       {
          printf ("%d \n", this->x[this->y]);
          this->y++;
       }
    }
    
    int main (void)
    {
       Data numbers; 
    
       numbers.total = 3;
       numbers.y = 0;
       numbers.x[0] = 5;
       numbers.x[1] = 3;
       numbers.x[2] = 1;
       
       {
          int i;
          for (i = 0; i < 10; i++)
          {
             Display (&numbers);
          }
       }
       system ("pause");
       
       return 0;
    }
    That produces:
    Code:
    5
    3
    1
    5
    3
    1
    5
    3
    Appuyez sur une touche pour continuer . . .
    This is part of a game engine for another platform. Its gonna be open source so I don't want people to have to pass in pointers. Its not for me to use. This is not the exact code but it shows what I'm trying to do.
    Last edited by ExtremelyStupid; 08-02-2004 at 03:59 AM.

  6. #6
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by ExtremelyStupid
    This is part of a game engine for another platform. Its gonna be open source so I don't want people to have to pass in pointers.
    Trust me, C-programmers are not stupid (well, most of them) and they are not scared by pointers. See how Gtk+ works. It's just how IMO C would be used.
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Replies: 4
    Last Post: 10-17-2002, 10:09 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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