Variable assigning Functions

This is a discussion on Variable assigning Functions within the C Programming forums, part of the General Programming Boards category; I know this might sound stupid, but I want to use a function to assign my own data type.. Code: ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    24

    Variable assigning Functions

    I know this might sound stupid, but I want to use a function to assign my own data type..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef unsigned char byte;
    
    typedef struct {
     byte header[15];
     int a;
     int b;
     byte *data;
    } MY_TYPE;
    
    int my_function(MY_TYPE mt)
    {
     
    
    }
    
    int main()
    {
     int er;
     MY_TYPE a;
    
     er = my_function(a);
    }
    What I want is for my_function to return an integer value to indicate the functions success or failure. This I know how to do..

    But I want my_function's My_TYPE mt, to be equal to the main's MY_TYPE a, after the function is called..

    Here's my problem, I want to do it without making ANY of the variables GLOBAL.

    Any help would be great..
    Keep smiling, it makes the big guys wonder what you're up to.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    15
    I'm a little confused by what your saying, but I am going to assume, you want to assign MY_TYPE a values through my_function.

    Since you pass my_function a pointer to MY_TYPE a, it should change the values within...

    I may be getting this confused with Java, but have you tried to write your function yet? -- If you have did it not work?

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    24
    It is as follows..

    Code:
    int my_function(MY_TYPE mt)
    {
     mt = get_data();  //  A custom function that assigns the variables  of   mt from a file  No need to post that code, it is irrelevant to what I want.
    
     if((strcmp(mt.header, "###MY   TYPE###") != 0)
     {
      return -1; // Unsuccessful
     } 
     else
     {
      return 0;  // Successful.. but I also want it to assign mt's value to main's a. As main's a is empty.
     }
    }
    Last edited by emus21; 11-08-2003 at 09:35 PM.
    Keep smiling, it makes the big guys wonder what you're up to.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    161
    Pass a pointer:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef unsigned char byte;
    
    typedef struct {
     byte header[15];
     int a;
     int b;
     byte *data;
    } MY_TYPE;
    
    int my_function(MY_TYPE *mt)
    {
      *mt = get_data();
      // do other stuff here
    }
    
    int main()
    {
     int er;
     MY_TYPE a;
    
     er = my_function(&a);
    }

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    24
    I suppose I should have done more research before posting..

    pointers, duh, I am an idiot..

    Thanks much for the help,
    Keep smiling, it makes the big guys wonder what you're up to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning an object variable to a constant reference
    By Canadian0469 in forum C++ Programming
    Replies: 18
    Last Post: 11-10-2008, 09:48 AM
  2. Assigning Binary Value to a Variable
    By JAYMAN in forum C Programming
    Replies: 2
    Last Post: 11-06-2005, 12:24 PM
  3. Calling functions via variable
    By SoundFX in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2005, 10:26 PM
  4. variable modification inside functions
    By point in forum C++ Programming
    Replies: 6
    Last Post: 06-17-2005, 08:36 PM
  5. Passing a function name as a variable to execute it?
    By Zuul in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 11:42 AM

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