Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: [C] The lousy random question thread

  1. #1
    Sergeant serthy's Avatar
    Join Date
    Nov 2012
    Posts
    450
    Thanks
    96
    Thanked 296 Times in 188 Posts

    [C] The lousy random question thread

    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..

  2. #2
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,010
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts
    hey, 2nd C question, yee

    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.

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

    PHP Code:
        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:

    PHP Code:
    float delta 1
    every compiler is smart enough to recognise, what you want.
    timescale 0.01

  3. The Following User Says Thank You to kung foo man For This Useful Post:

    serthy (1st April 2013)

  4. #3
    Sergeant serthy's Avatar
    Join Date
    Nov 2012
    Posts
    450
    Thanks
    96
    Thanked 296 Times in 188 Posts
    Thank you ^___^

    i talked to you (kung) in xfire, and at this ime i understood this snippit:
    Code:
    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 = (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:
    Code:
        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
    have fun :'D
    Last edited by serthy; 1st April 2013 at 14:03.

  5. #4
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,010
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts
    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
    timescale 0.01

  6. #5
    Sergeant serthy's Avatar
    Join Date
    Nov 2012
    Posts
    450
    Thanks
    96
    Thanked 296 Times in 188 Posts
    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

    Code:
    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)

  7. #6
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,010
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts
    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.
    timescale 0.01

  8. The Following User Says Thank You to kung foo man For This Useful Post:

    serthy (9th June 2013)

  9. #7
    Sergeant serthy's Avatar
    Join Date
    Nov 2012
    Posts
    450
    Thanks
    96
    Thanked 296 Times in 188 Posts
    Quote Originally Posted by kung foo man View Post
    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
    Code:
    	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.
    Last edited by serthy; 9th June 2013 at 15:52.

  10. #8
    Sergeant serthy's Avatar
    Join Date
    Nov 2012
    Posts
    450
    Thanks
    96
    Thanked 296 Times in 188 Posts

    [C++] methods excurs

    Hey, its me again..

    PHP Code:
    type foobar()
    {
        return (
    type)1337;
    }

    class 
    myClass
    {
        public:
            
    type x;

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

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

    float tmp;
    myClass c1 c2;

    foobar(); //calls function but returnvalue isnt used
    tmp c1.foobar42 );//calls methods
    c2.foobar42 ); //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
    Last edited by serthy; 2nd August 2013 at 22:04.

  11. #9
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,010
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts
    What stops you from doing that simple test? ^^
    timescale 0.01

  12. #10
    Sergeant serthy's Avatar
    Join Date
    Nov 2012
    Posts
    450
    Thanks
    96
    Thanked 296 Times in 188 Posts
    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)'
    Code:
    #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 );
    }
    Last edited by serthy; 2nd August 2013 at 22:13.

  13. The Following User Says Thank You to serthy For This Useful Post:

    kung foo man (2nd August 2013)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •