Passing Values to Functions

This is a discussion on Passing Values to Functions within the C Programming forums, part of the General Programming Boards category; how do i pass a value to a function? lets say i wanna pass an array of string to a ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    5

    Passing Values to Functions

    how do i pass a value to a function?
    lets say i wanna pass an array of string to a function

    char array[50];
    printf("Enter a string:")
    scanf("%s",array);

    how do i pass this array to a function like
    void InsertString(struct node**S,char array[50]{
    }
    ??

  2. #2
    Unregistered
    Guest
    If you are passing a pointer to a function:
    functionName(&variable);

    If you are passing an array:
    functionName(array);
    or
    functionName(&array[0]);

    Otherwise:
    functionName(variable);

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Look at this:


    Code:
    typedef struct Node
    {
     Node *next;
    
     char string[50];
     
     Node *prev;
    };
    
    
    int InsertString(Node *n, char *c, int maxlen)
    {
     int len = strlen(c);
    
     if(len > maxlen)
     return -1;
    
     if(len < 1)
     return -2;
     
     strcpy(n->string, c);
     
     return 1;
    }
    
    
    
    
    
    int main()
    {
    
    Node *thisNode = (Node *)malloc(sizeof(Node));
    
    if(thisNode == NULL)  //couldn't allocate...
    return 0;
    
    char string[] = "I am a programmer. But why?";
    
    int valid = InsertString(thisNode, string, 50);// struct allows 50 ch.
    
    if(!valid)
    {
     switch(valid)
     {
      case -1: printf("String was too big!");
      break;
      
      case -2: printf("String was empty!");
      break;
     
      default: printf("Undefined problem with string!");
      } 
    } 
    
    
    
    
    
    return 0;
    }


    Just a simple example of passing parameters, utilizing return values, and using switch statements...

    ...also notice that if you "typedef" a struct, you can declare variables of it's type and likewise declare in function parameters as if it were an int, double, etc. , that is, without using the "struct" keyword.

    ...happy coding.
    Last edited by Sebastiani; 12-25-2001 at 07:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM
  3. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 11:22 AM
  4. functions passing values
    By srinurocks in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2002, 11:49 PM
  5. Replies: 1
    Last Post: 01-20-2002, 10:50 AM

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