Thread: array of records -> function

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    array of records -> function

    Hi! I have an array of records, now the question is - is it possible to pass whole array (100 records) with all the records with a simple variable? I doubt that it's a good idea or even if it's possible (???) - another option, in my situation, is to read all the records from a file into an array directly inside a function, which could be easier, if I'm thinking correct (and again I don't know if C will allow it, cause I'm new to this language and haven't discovered everything here yet).

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Arrays degrade to a pointer to the type. So, an array of integers will be passed as a pointer to an int.
    Code:
    /* some struct... */
    struct foo
    {
        ...stuff...
    };
    ...
    
    /* some place... */
    struct foo fooarray[X];
    ...
    
    /* some function... */
    void bar( struct foo * array, size_t size )
    {
        ...stuff...
    }
    ...
    
    /* some function call... */
    bar( fooarray, X );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by keisx View Post
    Hi! I have an array of records, now the question is - is it possible to pass whole array (100 records) with all the records with a simple variable? I doubt that it's a good idea or even if it's possible (???) - another option, in my situation, is to read all the records from a file into an array directly inside a function, which could be easier, if I'm thinking correct (and again I don't know if C will allow it, cause I'm new to this language and haven't discovered everything here yet).
    We never pass an entire array in C. That's too inefficient. We pass a single pointer to the base of the array. Conveniently, that also happens to be the name of the array, itself.

    The name of the array is not a pointer than can have it's address changed to something else. It's constant, and will always point to the base of it's own array.

    Declare your array in the calling function, and then you can fill it in the called function. When the called function is done, the array will still be valid (not go out of scope and disappear).

    try this:

    Code:
    #include <stdio.h>
    
    int main(void) {
      char ch, myArray[] = { "The willow rustled in the wind"};
    
      printf("%s is at memory address %p", myArray, myArray);
    
      printf("\n\t\t\t     press enter when ready");
      ch = getchar(); 
      return 0;
    }
    That address is in hexadecimal, so it's not the usual numbers.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Million thanks quzah - that helped me a lot!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question: sizeof() problem in function
    By Xeyide in forum C Programming
    Replies: 3
    Last Post: 09-04-2009, 12:05 AM
  2. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM