Thread: eh..eXcuse me for this question.. I want to learn..

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    1

    eh..eXcuse me for this question.. I want to learn..


    Hello, I need your help with two things:

    a) passing variable by reference and by value, what's the differences? what's the uses?

    b) it seems like defining an array size must be done with only constans, is it really just this way? if not, how can I define
    one (array) with variables (like int A[x][y]?


    thank you very much in advance...

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: eh..eXcuse me for this question.. I want to learn..

    Originally posted by mindrebel
    b) it seems like defining an array size must be done with only constans, is it really just this way? if not, how can I define
    one (array) with variables (like int A[x][y]?
    Use dynamic allocation (new for C++ and malloc() for C). I'm not sure about the syntax for malloc() (always used new ), but it is something like this:
    Code:
    //Create a pointer
    int* MyPointer;
    
    //Allocate memory
    MyPointer=malloc(SizeX*SizeY);
    
    //Check if the allocation was successful
    if(MyPointer!=NULL)
    {
    
       //*** Do whatever you want ***
       MyPointer[y*SizeX+x]=56;
    
       //Deallocate the memory. If you don't do this, you
       //will end up with memory leaks. ONLY deallocate if it
       //has been allocated, might crash otherwise...
       free(MyPointer);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70

    lol~

    (1):there no passing values for reference in 'C', it's C++; and passing a value by reference is the same as creating an shortcut(alias) for a program in a folder to your desktop, in this case for a variable.

    (2): use * if your compiler does not accept variable size arrays
    e.g.: numbers[*][*];
    better would be try using malloc and calloc to dynamically allocate memory for your arrays.
    Ünicode¬>world = 10.0£

  4. #4
    Unregistered
    Guest
    Generally, passing by value means to send a copy of something. Passing by reference means to send the address in memory. The advantage of passing by reference is that there is no limit to what you can pass by reference.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: lol~

    Originally posted by bljonk
    (2): use * if your compiler does not accept variable size arrays
    e.g.: numbers[*][*];
    better would be try using malloc and calloc to dynamically allocate memory for your arrays.
    Is this ANSI standard? I've never seen it used.

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

  6. #6
    Unregistered
    Guest
    If you define an array as A [x][y], how does the compiler know how much memory to reserve?

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Me too. But perhaps he means "**numbers;".

    >If you define an array as A [x][y], how does the compiler know
    >how much memory to reserve?

    You can't define an array like that. You can define it for example as

    int **array;

    and then use memory allocation functions to create the array.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Shiro
    Me too. But perhaps he means "**numbers;".

    >If you define an array as A [x][y], how does the compiler know
    >how much memory to reserve?

    You can't define an array like that. You can define it for example as

    int **array;

    and then use memory allocation functions to create the array.
    I suspect it depends on the compiler. Oddly enough:

    int x[*][*]; //Yes, JUST LIKE THAT!

    Actually compiles without warning or error in MSVC++!

    Additionally, some compilers will allow the following as valid working code:
    Code:
    int main( void )
    {
        int x;
        int y;
        x = promptforx( );
        y = promptfory( );
    
        {
            int array[x][y];
            ...not use array as a normal array...
        }
        return 0;
    }
    I'm sure both are some odd compiler specific behaviour.

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

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >a) passing variable by reference and by value, what's the differences? what's the uses?
    There's no such thing as passing by reference in C. Everything is passed by value, but passing by reference can be simulated by passing a pointer instead of the original value. The pointer is passed by value, but still points to the object, so it appears to be passing by reference.

    >b) it seems like defining an array size must be done with only
    >constans, is it really just this way? if not, how can I define
    >one (array) with variables (like int A[x][y])?
    Static arrays in C89 must use a constant for the size, to create an array at runtime with a variable you must create a dynamic array by allocating the memory for it yourself from a pointer. The new C99 standard allows static arrays to be created with variable values however.

    >use * if your compiler does not accept variable size arrays
    This is implementation dependent and most definitely NOT standard.

    >If you define an array as A [x][y], how does the compiler know how much memory to reserve?
    In C89 it doesn't and will flag an error. In C99 a constant value isn't required so the compiler simply takes the current values of x and y and uses them to allocate memory for the array.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  2. Just starting to learn C++ Question about classes
    By uraliss in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2006, 03:31 AM
  3. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM