Thread: Another newbie Pointer/Array question!

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    4

    Another newbie Pointer/Array question!

    I am having trouble interpreting certain snippets in the code below (the omitted code is hardware specific code).

    I have had a look on the tutorial regarding pointers & arrays but still couldn't figure it out.

    I have listed the code with comments of my current understanding of each statement and would be much obliged if someone could clarify what they actually do.

    Thanks.


    Code:
    int *perform(int *w)               //perform takes pointer to an int as arg & returns the same type
        
       {
                float *out[1];             // An array of pointers to floats with 1 element
                  
                int n = (int)(w[3]);     //  Why does w become an array suddenly??
    
                  out[0] = (float *)(w[2]);    // Array cast to type pointer to float
    ....
    
                  *out[0]++=0;                   //  Does this set each element in out[] to 0 on each cycle? what relevance does the * have here?

  2. #2
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Code:
    int *perform(int *w)               //perform takes pointer to an int as arg & returns the same type
    w is a pointer to an int. Arrays is reached this way; a pointer to the first element is used to reach all arrays. In reallity, writing myarray[i] is simply the same as writing *(myarray + i). Every array that is not allocated when entering the scope Is created by calling some allocating function like malloc or calloc. Those functions really returns a pointer to the first element so you got to store a pointer to the array to be able to reach it.

    In this case you will get an int by calling w[i] since w is of the type pointer to int, on the other hand I have no idea why they typecast an int to a float*. Maybe they are float pointers stored as ints, that would explain why they try to reach an object through out[0]. Where did you get the code?
    Come on, you can do it! b( ~_')

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  4. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM