Results 1 to 4 of 4
  1. #1
    bkRy's Avatar
    Join Date
    Sep 2016
    Gender
    male
    Location
    Austria. (No kangaroos)
    Posts
    15
    Reputation
    14
    Thanks
    7
    My Mood
    Amazed

    Question Pointer & putting values into the parameters

    So I've put x into () and gave it the value 1000, well after printing the value I got 0.
    1.Why isn't it outputting 1000?
    2.Why do I have to give test(i) the value "int i = 0;"? and why can't I use x?

    Code:
    #include <iostream>
    #include <Windows.h>
    
    void test(int x = 1000){	
    
    			std::cout << "x1: "<< x << std::endl; //x = 0 if declared in parameters
    
    	int *y = &x;
    	*y = x / 2;
    
    			std::cout << "x2: " << x << std::endl; //well 0/2 = nothing
    
    	printf("\n x = %p; y = %p \n", y, x);
    }
    
    
    int main()
    {
    	int i = 0;
    	test(i); //why do I have to init. a random value into test() to get it working, why cant I use x? 
    	getchar();
    }


    But if I do it like that:

    Code:
    #include <iostream>
    #include <Windows.h>
    
    void test2(){
    		int x = 1000;
    
    		std::cout << x << std::endl;
    
    		int *y = &x;
    		*y = x / 2;
    
    		std::cout << *y << std::endl;
    		printf("\n x = %p; y = %p \n", y, x);
    }
    
    int main()
    {
    	test2();
    	getchar();
    }
    The programm runs perfectly fine, with one exception.
    Shouldn't the adress for y be the same as for x, since I'm just overwritting the a value in that specific address?

    What am I doing wrong?

  2. #2
    WasserEsser's Avatar
    Join Date
    Jul 2015
    Gender
    male
    Posts
    735
    Reputation
    174
    Thanks
    677
    My Mood
    Busy
    If you do something like void test(int x = 1000), you give the parameter a default value, which it will get initialized to if you do not pass it.

    This means, that you could have something like this:

    int add( int x, int y = 1000 )

    If you call add with two arguments ( add( number1, number2 ) ), it will use the two arguments passed in within the function, but if you only pass one argument, it will default initialize the second argument to 1000. add( number1 ) is perfectly valid here, it will use y initialized as 1000.

    Since you pass a number to the function, it will use your number instead of the default initialization.

    //why do I have to init. a random value into test() to get it working, why cant I use x?
    C++ does not guarantee you default initialized variables, which means that if you do not give a variable a value, it will be "initialized" to whatever is at the stack when creating the variable, though i think most compilers default initialize them for you under the hood, but keep in mind that it's not a C++ standard and that C++ alone does not guarantee you default initialized variables.

    Shouldn't the adress for y be the same as for x, since I'm just overwritting the a value in that specific address?
    y holds an address as value, which is the address of x. A variable has an address, a value and a type. Your pointer is of type int pointer and has some address windows gives your variable at that point in time. The value of your pointer is the address ( the memory address windows gives your variable ) of the variable x. The address of y is different from the address of x, though the value of y is equal to the address of x.
    Last edited by WasserEsser; 10-31-2016 at 11:49 AM.

  3. The Following User Says Thank You to WasserEsser For This Useful Post:

    bkRy (10-31-2016)

  4. #3
    bkRy's Avatar
    Join Date
    Sep 2016
    Gender
    male
    Location
    Austria. (No kangaroos)
    Posts
    15
    Reputation
    14
    Thanks
    7
    My Mood
    Amazed
    Thank you,
    now I get how these argument calls work in there + the overwritting !

  5. #4
    Hugo Boss's Avatar
    Join Date
    Oct 2011
    Gender
    male
    Posts
    28,752
    Reputation
    4790
    Thanks
    5,902
    My Mood
    Angelic
    Seems like you got things solved?

     
    Super User since 08-29-2017
    Global Moderator from 10-02-2016 - 08-29-2017
    Premium Seller since 11-16-2016
    Moderator from 09-24-2015 - 01-09-2016
    Alliance of Valiant Arms Minion from 11-12-2015 - 01-09-2016
    Market place Minion from 09-24-2015 - 01-09-2016
    Crossfire Minion from 09-11-2015 - 01-09-2016

    Middleman from 07-07-2015 - 01-09-2016
    Market Place Minion from 03-03-2014 - 08-01-2014
    Middleman from 01-30-2014 - 08-01-2014
    Moderator from 03-29-2013 - 04-04-2013
    Market Place Minion from 03-07-2013 - 04-04-2013
    Premium Member since 01-25-2013
    Middleman from 12-04-2012 - 04-04-2013
    Registered since 10-9-2011

Similar Threads

  1. [Solved] How do i put custom items into the client so it wont make other players lag.
    By MikeRaarupBirk in forum Realm of the Mad God Private Servers Help
    Replies: 6
    Last Post: 05-10-2016, 02:04 PM
  2. [Tutorial] How to put the long hex values like the weapons XP into vb.net
    By distiny in forum Call of Duty Modern Warfare 3 Tutorials
    Replies: 8
    Last Post: 11-24-2011, 05:34 AM
  3. [Help] How to get the value of the address, a pointer is pointing at? [Solved]
    By Arnibold in forum C++/C Programming
    Replies: 12
    Last Post: 09-05-2011, 02:23 AM
  4. how do i put screen shot into the description part?
    By ducksfan1996 in forum Combat Arms Help
    Replies: 5
    Last Post: 01-18-2010, 01:19 AM
  5. no fall damage addresse and pointer or value
    By 123456789987654321 in forum WarRock - International Hacks
    Replies: 2
    Last Post: 06-28-2007, 06:04 PM