Gamestudio Links
Zorro Links
Newest Posts
Zorro version 3.0 prerelease!
by Grant. 02/24/26 22:21
WFO Training with parallel cores Zorro64
by Martin_HH. 02/24/26 19:51
ZorroGPT
by TipmyPip. 02/23/26 21:52
Camera always moves upwards?
by clonman. 02/21/26 09:29
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 02/19/26 13:22
AUM Magazine
Latest Screens
Dorifto samurai
Shadow 2
Rocker`s Revenge
Stug 3 Stormartillery
Who's Online Now
5 registered members (TipmyPip, clint000, Grant, chsmac85, Martin_HH), 5,858 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
alx, ApprenticeInMuc, PatrickH90, USER0328, Sfrdragon
19199 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Very confused about pointers #289092
09/10/09 13:41
09/10/09 13:41
Joined: Dec 2008
Posts: 19
N
number_one Offline OP
Newbie
number_one  Offline OP
Newbie
N

Joined: Dec 2008
Posts: 19
My pointers have been pointing crazily for the past few days, and I am getting crazy as well. Usually, I would not be so troubled by pointers, but all of a sudden, after trying to make a pretty complex set of functions with many pointers, my brain is now... not at its best and I feel I must ask these questions...


Firstly, have a look at this code. Does it make any sense?

float* arr1;
arr1=(float*)realloc(arr1,5*sizeof(float));
arr1[0]=5;
arr1[1]=15;
arr1[2]=25;

Am I doing this correctly? AM I allowed to treat float* which i malloc - ed like an array? arr1[0] sets the value of the first block to 5 right, or am I pointing the block of memory to memory address "5" ?

And....


void testfunc(float* varA,var varB, var varC)
{

printf("%i",(int)varA); //anything wrong with this?
if (varB>*varA[0]) //or this?
{
//run non-pointer related stuff
}
varA[1] = varC; //or this?
}

Of course, the first question is more important, since my problem is probably due to some confusion with the way pointers work.

I have worked with pointers before, but my absense from playing around with programming (hence forgetting pointers) is definately causing my problems. Any sites which offer a quick, painless, accessible, non-frightening crash course on lite-c pointers (which I dunno... seems to work differently from C++, or are they the same? I don't know anymore) to refresh my memory?

Re: Very confused about pointers [Re: number_one] #289097
09/10/09 14:17
09/10/09 14:17
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Section 1 ::
It is basically, but very unsafely done, and very unlikely to actually work.
Here is two ways for it to be done safely.
Im unsure which is best for your overall situation though.
You are using 're-alloc' in a strange way....
Code:
float* arr1=NULL;
   arr1=(float*)realloc(arr1,5*sizeof(float));
   arr1[0]=5;
   arr1[1]=15;
   arr1[2]=25;

//OR my preferred way
   float* arr1;
   arr1=(float*)malloc(5*sizeof(float));
   arr1[0]=5;
   arr1[1]=15;
   arr1[2]=25;



Section 2 ::
Not too sure heres, its a fuzzy area for me too. But I THINK this is the way to go.
Code:
void testfunc(float* varA,var varB, var varC) 
{
   printf("%i",(int)*varA);
   if (varB>*varA[0])
   {
      //run non-pointer related stuff
   }
   *varA[1] = varC; //or this?
}




"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Very confused about pointers [Re: EvilSOB] #289103
09/10/09 15:18
09/10/09 15:18
Joined: Dec 2008
Posts: 271
Saturnus Offline
Member
Saturnus  Offline
Member

Joined: Dec 2008
Posts: 271
Nothing more to add to the first question, I think.

Quote:
AM I allowed to treat float* which i malloc - ed like an array? arr1[0] sets the value of the first block to 5 right, or am I pointing the block of memory to memory address "5" ?

Yes, you are doing this right.

arr1 alone (without any index) just points to the base address of the array or rather the allocated memory block.

However, if you use the indirection operator on the base address, you will get the first value: *arr1. *arr1 is equal to arr1[0]. Consequently arr[i] is the same as *(arr + i), so its nothing more than the indirected base address shifted by the index i.


With this said, your second question can be answered as well: if 'float* varA' is an array, you don't have to use the indirection operator in the function, as varA is a pointer to the base address and varA[i] is equal to *(varA + i), thus it's already indirected.

So in regard to your function:

- 'printf("%i",(int)varA)' would print the base address,
- 'if (varB>*varA[0])' is an illegal indirection and
- 'varA[1] = varC' is all right. : )

This does not apply if 'float *arrA' is NOT a pointer to an array but a single float, though. Then indirection is needed.

Hope this helps (even though my post seems to be pretty messy).

BTW: Pointers in lite-C should behave like in C as long as PRAGMA_POINTER is defined.

Re: Very confused about pointers [Re: Saturnus] #289168
09/11/09 00:48
09/11/09 00:48
Joined: Dec 2008
Posts: 19
N
number_one Offline OP
Newbie
number_one  Offline OP
Newbie
N

Joined: Dec 2008
Posts: 19
Thanks both! I'm starting to see the light. Cleared up some stuff but there are still some things I don't get. I'm trying to use realloc to change the size of my array at runtime.

Anyway, another piece of code I need to clarify about

Code:
void arrinitialise(float* arr1)
{
   arr1=malloc(5*sizeof(float));
   arr1[0]=0;
}

void main()
{
level_load("");
float* arr1=NULL;
float* arr2=NULL;
float* arr3=NULL;
float* arr4=NULL;
float* arr5=NULL;

arrinitialise(&arr1);                     //this doesn't work
arrinitialise(&arr2);      
                           
//arr1=malloc(5*sizeof(float));           //this works
 //  arr1[0]=0;


}


Please excuse any foolishness by me, since I get the feeling that I'm supposed to know what is already wrong from the posts above. But I don't.

My problem is that my initialisation function(arrinitialise) somehow crashes(but without using a function, the same piece of code, the two lines I commented out, works). So probably, the problem lies in what my pointer points to?

Thanks!

Re: Very confused about pointers [Re: number_one] #289182
09/11/09 07:26
09/11/09 07:26
Joined: Dec 2008
Posts: 271
Saturnus Offline
Member
Saturnus  Offline
Member

Joined: Dec 2008
Posts: 271
Quote:
arrinitialise(&arr1); //this doesn't work

arr1 is a pointer and you are using the address operator on it. So a pointer to a pointer is returned.

Thus your initialization function should look like this:

Code:
void arrinitialise(float** arr1)
{
   *arr1=malloc(5*sizeof(float));
   *arr1[0]=0; // same as **arr1 = 0
}



Re: Very confused about pointers [Re: Saturnus] #289348
09/12/09 07:07
09/12/09 07:07
Joined: Dec 2008
Posts: 19
N
number_one Offline OP
Newbie
number_one  Offline OP
Newbie
N

Joined: Dec 2008
Posts: 19
I see, that made sense! But I'm still a little lost. Should it not be

Code:
void arrinitialise(float** arr1)
{
   arr1=malloc(5*sizeof(float));
   *arr1[0]=0; // same as **arr1 = 0
}



instead? Since *arr1 would still be a undefined value rather than the address?

All these pointer to pointer thing is really confusing and hard to work with.. Is there a better way to maintain an array at runtime or for me to write these code?

Re: Very confused about pointers [Re: number_one] #289377
09/12/09 13:13
09/12/09 13:13
Joined: Dec 2008
Posts: 271
Saturnus Offline
Member
Saturnus  Offline
Member

Joined: Dec 2008
Posts: 271
Quote:
Should it not be
Code:
void arrinitialise(float** arr1)
{
   arr1=malloc(5*sizeof(float));
   *arr1[0]=0; // same as **arr1 = 0
}


instead? Since *arr1 would still be a undefined value rather than the address?

arr1 is a pointer to a pointer. A pointer to a float pointer to be exact. So if you change arr1 like 'arr1 = ...' the first of these two pointers is changed. However, this is not what you want here: you want to change the second pointer instead (that is the local pointer of your main function). To get this pointer, the indirection operator has to be used: *arr1 = malloc... : )

Quote:
All these pointer to pointer thing is really confusing and hard to work with.. Is there a better way to maintain an array at runtime or for me to write these code?

Depends on what you would like to achieve.
An easy way would be to encapsulate your array and corresponding data in a struct.

Like this for example:
Code:
typedef struct FLOAT_ARRAY
{
    float *data; // allocated memory
    int    size; // number of elements
} FLOAT_ARRAY;

// Returns a new float array
void *float_array_new(int size)
{
    FLOAT_ARRAY *array = malloc(sizeof(float) * size);
    array->size = size;
    return array;
}



Then you could use it like this:

FLOAT_ARRAY *arr = float_array_new(10);
(arr->data)[0] = 123.456; // set first element
(arr->data)[arr->size - 1] = 987.654; // set last element

Now you don't have to pass pointers to pointers anymore. You can just pass 'arr' and change its members within other functions. In addition you can keep track of the arrays actual size easily.


Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1