PDA

View Full Version : [C] The lousy random question thread



serthy
28th March 2013, 16:33
Hey

Im currently trying to learn abit more about C (a start-off) and i do have some questions:

1.)
int a = 10;
void *p;
p = (int *)&a;

p is the (void)pointer
p is casted to int
p gets the adress of a assigned (&a)
> but why do i have to write it this way: (int *)&a and not (int)*(&a)?
> can some1 explain me the syntax of this please?
> would be (int*)&a be the same, without the space between int and *?
> what is int* then?
> is the dereferenc-star * assigned to the int-cast or to the adress here?
here it is used again:
int **matrix;
p = malloc( count * sizeof( int * ) );
size of what? an int-pointer?
the most confusing part is this:
int x = 10;
void *p;
p = (int *)&x;
*(int *)p = 100;
wtf... maybe ill get it someday

2.) do i have to write numbers with their special letter on the end?
float x = 1.5f; or is float x = 1.5; the same?
same with long, hex etc..

kung foo man
28th March 2013, 17:03
hey, 2nd C question, yee :D

1)


> but why do i have to write it this way: (int *)&a and not (int)*(&a)?

everything in the () is nothing but a *type*. so (int *) is just representig the type of a int-pointer.



> would be (int*)&a be the same, without the space between int and *?

yep, totally the same


> is the dereferenc-star * assigned to the int-cast or to the adress here?

in the context of a cast (int *) it is *not* dereferencing anything. it just indicates that is *is* a pointer and can be referenced later. :D

you can derefence references as often as you want and make jokes out of it:



int i = 4;
printf("%d", *&*&*&*&i); // prints 4

// thats the same:
printf("%d", *(int *)&*&i); // 4





p = malloc( count * sizeof( int * ) );
size of what? an int-pointer?


yea, the size of a int-pointer. on 32bit = 4 byte, on 64bit = 8byte
p is a pointer-on-a-pointer... when you derefence it once, it is still a pointer, so you need to derefence it twice (**p). it's not needed in that example. ^^

2) you dont need to write any 1.0 or 1.0f

you can just write:


float delta = 1;

every compiler is smart enough to recognise, what you want. :)

serthy
1st April 2013, 12:55
Thank you ^___^

i talked to you (kung) in xfire, and at this ime i understood this snippit:

float x = 10.3;
void *p;
p = (float *)&x;
*(float *)p = 100.001;
but for now, i dont get the 3rd line..
p is a void* and gets an address assigned from a floating point number, which is casted to a float*
a void* can contain a float* without any troubles? (yh, maybe the compiler knows how to handle this, but i want to learn how to do it correctly)

if i initialise a void* p; on startup, does p always stay a void* or is this possible:p = (int *)p; to cast the variable itself to another pointer-type? (same with normal types: int i = 1; i = (float)i;)

i just re-read you answer:

int i = 4;
printf("%d", *&*&*&*&i); // prints 4
// thats the same:
printf("%d", *(int *)&*&i); // 4

i assume on here that (int *)&i == &i am i correct?
and if so, thn on my first example (float *)&x == &x means, &x still is a float* and thn there is no need for doing a cast b4?

yh, i know, ima N00b, but if youre willing to answer me, feel free to :D
have fun :'D

kung foo man
1st April 2013, 13:11
a void* can contain a float* without any troubles? (yh, maybe the compiler knows how to handle this, but i want to learn how to do it correctly)


yea, a pointer can contain every other type of pointer, becaues its internally just 32/64 bit reference. So it doesnt contain data, it just points to it.




if i initialise a void* p; on startup, does p always stay a void* or is this possible = (int *)p; to cast the variable itself to another pointer-type? (same with normal types: int i = 1; i = (float)i


not possible, but if you need the value of a variable as another type, just cast it. ^^




i assume on here that (int *)&i == &i am i correct?


yep



and if so, thn on my first example (float *)&x == &x means, &x still is a float* and thn there is no need for doing a cast b4?


also correct, you are comparing the same types there

serthy
9th June 2013, 14:03
Hey! ;D

i got another one (C++):

i started an empty project on c::b and got some sourcecode to play with
the code seems to be designed for win32-apps
so __stdcall is before every callback function (i know what this does, but not when exactly it is required)
my question: on any other environment is this calling convention ignored?
is it valid to remove them in my empty project? i did so, and it worked, but maybe i run in troubles later


static VOID __stdcall resUnloadRaw( LPRESFILELUMP lpLump ) //load and save funcs are similar
{
delete [] PCHAR( lpLump->lpData );
lpLump->lpData = NULL;
}

BOOL ResFile::RegisterHandler( DWORD dwType ,
BOOL __stdcall (*lpLoad)( fstream* , LPRESFILELUMP ) ,
BOOL __stdcall (*lpSave)( fstream* , LPRESFILELUMP ) ,
VOID __stdcall (*lpUnload)( LPRESFILELUMP ) )
{
...
}

foo()
{
RegisterHandler( 0 , resLoadRaw , resSaveRaw , resUnloadRaw );
}

next:
is there a reason for me not to use windows typedefs a la DWORD? (i guess im not going to use the winAPI)

kung foo man
9th June 2013, 14:14
The function is used as callback, so it's nice to explicitly set the calling convention. But since its stdcall (=standard calling convention), its not really needed.

Every big "project" defines their own types like DWORD (WinAPI) for "unsigned int". I suggest you use just the plain "unsigned int", so you dont depend on windows-include-files.

serthy
9th June 2013, 14:46
The function is used as callback, so it's nice to explicitly set the calling convention. But since its stdcall (=standard calling convention), its not really needed.

Every big "project" defines there own types like DWORD (WinAPI) for "unsigned int". I suggest you use just the plain "unsigned int", so you dont depend on windows-include-files.

Okay, i guess its better to stay on these conventions, since they are only for callbacks (at least for me now)

EDIT: i solved the next after looking over it for some minutes ;D

resFileLumpHandler_sp lpLumpHandler = lumpHandlerListPtr , lpTemp; // Delete the lump handler list.
// is the same as:
resFileLumpHandler_sp lpLumpHandler = lumpHandlerListPtr; // Delete the lump handler list.
resFileLumpHandler_sp lpTemp; // Delete the lump handler list.

serthy
2nd August 2013, 19:16
Hey, its me again..


type foobar()
{
return (type)1337;
}

class myClass
{
public:
type x;

type foobar( float value )
{
type t = value * 2;
std::cout << "type foobar( float value )" << std::endl;
return t;
}

void foobar( float value )
{
x = value;
std::cout << "void foobar( float value )" << std::endl;
}
};

float tmp;
myClass c1 , c2;

foobar(); //calls function but returnvalue isnt used
tmp = c1.foobar( 42 );//calls methods
c2.foobar( 42 ); //does the compiler use the method without a returnvalue?

i know that the compiler generates different function-identifiers for both methods (different in and output types)

but my question is: does the compiler/cpu know when to use a method with or without the returnvalue?



EDIT
maybe these console prints will brigthen things up

kung foo man
2nd August 2013, 20:53
What stops you from doing that simple test? ^^

serthy
2nd August 2013, 21:02
not having a compiler atm xD

(edited the question above)


EDIT:
http://www.compileonline.com/compile_cpp_online.php
main.cpp:21:10: error: 'void myClass::foobar(float)' cannot be overloaded
main.cpp:14:13: error: with 'myClass myClass::foobar(float)'


#include <iostream>

bool foobar( float x )
{
std::cout << "" << std::endl;
return true;
}

class myClass
{
public:

myClass foobar( float x )
{
std::cout << "myClass foobar()" << std::endl;

return myClass();
}

void foobar( float x )
{
std::cout << "void foobar()" << std::endl;

}
};

int main( int argC , char* args[] )
{
myClass c1 , c2 , c3;

foobar( 0.0f );
c1.foobar( 0.0f );
c2 = c3.foobar( 0.0f );
}

serthy
15th August 2013, 20:11
next 'problem':

i've got a struct including an array
this array should be variable in its length , but <= MAX_LENGTH


#define MAX_LENGTH 256

struct myStruct
{
int array[MAX_LENGTH];
};

infact this struct is now filled with 256 unused integers
how do i get anything similar to this without having a big empty array?

EDIT:
and i dont want to allocate new memory for every single input
so neither 'int* array;' nor 'int array[];' will work without that and without using static construction like: 'struct myStruct s = { 1 , 2 , 3 };'

kung foo man
15th August 2013, 21:43
If you want to have a variable length, you need to use realloc(): http://www.cplusplus.com/reference/cstdlib/realloc/

The function doesn't allocate new memory, it's just "extended" the old memory (so you don't need to copy the old data to a new malloc()-block etc.)

I guess you don't want that also, but there is no other way besides using some external libraries like GLib (http://en.wikipedia.org/wiki/GLib)

serthy
15th August 2013, 22:04
damn... you are right

i just read that VLA's (variable-length-arrays) arent allowed in structs either...
i guess have to go with the fixed array length and shipping unused array-elements around
this app is for my c++ anim system and i want my vertex to have up to MAX_BONES_PER_VERT
bonesID's stored for vertex blending purposes

is it worth to allocate each bonesID in my vertex-class or simply allocate the whole vertex (and its members) only once?
kung, you are the yoda in here ;)

EDIT: maybe i can go with sth like that:

class myVert
{
public:

float* weights;
unsigned char* bones;
unsigned char numInfluencers;

//constructor
myVert( const unsigned char count , const unsigned char* bonesArray = NULL , const float* weigthsArray = NULL )
{
numInfluencers = count;

bones = new unsigned char [numInfluencers];
weights = new float [numInfluencers];

for( int i = 0 ; i < numInfluencers ; i++ )
{
bones[i] = bonesArray[i];
weights[i] = weigthsArray[i];
}
}

~myVert() //destructor
{
delete [] bones;
delete [] weights;
}
};

kung foo man
15th August 2013, 23:02
i just read that VLA's (variable-length-arrays) arent allowed in structs either...


That's bullshit, a resizeable array is just a simple pointer and can be stored in a structure or wherever you want.

Well, for your other question, no clue ^^