read int from char * question

Started by
39 comments, last by ApochPiQ 6 years, 10 months ago

No, it uses address of a 32bit/64bit pointer

index is a reference, an address to memory where the passed parameter is

It really sounds like you don't understand the difference between references and pointers.

Can you, without testing, tell me what the following quick code would output? Be as specific as you are able to.


#include <iostream>

void myFirstFunction(int &value)
{
    std::cout << value << std::endl;
}

void mySecondFunction(int *value)
{
    std::cout << value << std::endl;
}

int main()
{
  int myInt = 7;
  
  myFirstFunction(myInt);
  mySecondFunction(&myInt);
}

Hello to all my stalkers.

Advertisement

It really sounds like you don't understand the difference between references and pointers. Can you, without testing, tell me what the following quick code would output? Be as specific as you are able to.

I know the difference, it sounds you seem to not pick it up from me, I'll try to phrase it in a more definitive way then:

-Reference provides adress of a variable/ a pointer to the memory of the variable.

-Pointer is a whole unsigned integer number, that is an address in memory, can be of 4 or 8 bytes large.

int value=1;

myFirstFunction(value);/*Console is written some whole number that points to the momory where 4 bytes on "value are"*/

myFirstFunction(1); /* Console is written some whole number that points to the memory where 4 bytes of a stacked local param is */

mySecondFunction(&value); /*Console is written some whole number that points to the memory where 4 bytes on "value are"*/

int main()
{
int myInt = 7;

myFirstFunction(myInt); // as I said, address of myInt
mySecondFunction(&myInt);// as I siad, same number
}

I know the difference, it sounds you seem to not pick it up from me.

No, you do not know, as demonstrated by your answer:

int value=1; myFirstFunction(value);/*Console is written some whole number that points to the momory where 4 bytes on "value are"*/ myFirstFunction(1); /* Console is written some whole number that points to the memory where 4 bytes of a stacked local param is */

Blantantly wrong.


int value = 1;
myFirstFunction(value);

Will print out "1". References behave exactly like value/variables wherever they are used. In order to get the address of the thing the reference points to, you have to promote it to a pointer via &. (&value).


myFirstFunction(1);

This won't even compile (on a standard-comformant compiler). You cannot bind a temporary to a non-const reference.

It really sounds like you don't understand the difference between references and pointers. Can you, without testing, tell me what the following quick code would output? Be as specific as you are able to.

I know the difference, it sounds you seem to not pick it up from me, I'll try to phrase it in a more definitive way then:

-Reference provides adress of a variable/ a pointer to the memory of the variable.

-Pointer is a whole unsigned integer number, that is an address in memory, can be of 4 or 8 bytes large.

int value=1;

myFirstFunction(value);/*Console is written some whole number that points to the momory where 4 bytes on "value are"*/

myFirstFunction(1); /* Console is written some whole number that points to the memory where 4 bytes of a stacked local param is */

mySecondFunction(&value); /*Console is written some whole number that points to the memory where 4 bytes on "value are"*/

int main()
{
int myInt = 7;

myFirstFunction(myInt); // as I said, address of myInt
mySecondFunction(&myInt);// as I siad, same number
}

You are wrong.

http://cpp.sh/2thd3

Press the "Run" button (lower-right) and see the output. This is the correct and expected behavior.

Hello to all my stalkers.

In order to get the address of the thing the reference points to, you have to promote it to a pointer via &. (&value).

In my knowledge, if a parameter is declared as Type &vari , and recieves an instance of Type on itself, it will provide sole address of that variable. Either- it is not actual anymore, or, it hasn't been true about basic integrated types of the language?

I have gonne to the web-page, and for the 98 standard, the one before C++11, compilation hangs on work, if I apply a custom type instead of int type to the inspected variable. Your observation about a need to reference & before the passed variable could not apply to not-value passed instances to parameters, I am still investigating this issue.

References behave exactly like value/variables wherever they are used. In order to get the address of the thing the reference points to, you have to promote it to a pointer via &. (&value).

Very inccorrect generalization, you must admit that's actualy omitting a reference existance?

So this code hang on me for the very link link, I alter only compilation standard to first one.


#include <iostream>

class SomeType{
    public:
    SomeType(){}
    int a;
    int b;
    int c;
    };
    
void myFirstFunction(SomeType &value)
{
    std::cout << value << std::endl;
}

void mySecondFunction(SomeType *value)
{
    std::cout << value << std::endl;
}

int main()
{
  SomeType myInt();
  
  myFirstFunction(myInt);
  mySecondFunction(&myInt);
}
Very inccorrect generalization, you must admit that's actualy omitting a reference existance?

What are you talking about?

I said references behave like a value-type, of course they point to another variable, but the point is this - an operation cannot possibly aquire the address of whatever the reference is pointing to, without promoting to pointer.

All of this:


int x = 1;
int& y = x;

y += 1; // does NOT increment the address
std::cout << y;
y = 5; // does NOT set the address to 5
printf("%i", y);

Is functionally 100% equivalent to:


int x = 1;

x += 1;
std::cout << x;
x = 5;
printf("%i", x);

The reference acts as an alias to another variable. Sure its underneath value is a pointer like 0xAB6492F, but whenever you perform another operation on it, it performs the operation on the original variable, as if the reference was a pointer that has been wrapped with "(*pValue)" . If you want to get the address, you need to use &.

EDIT:

So this code hang on me for the very link link, I alter only compilation standard to first one.

Yeah, this code does not compile. First off:


SomeType myInt();

Does not create an instance of SomeType, but a SomeType (*)() function pointer. Change it to:


SomeType myInt;

Plus:


void myFirstFunction(SomeType &value)
{
    std::cout << value << std::endl; // compilation error
} 

I'm not going to explain why this fails. Take what I explained about references, and you should see for yourself why cout cannot print SomeType&, but can print SomeType*.

n my knowledge, if a parameter is declared as Type &vari , and recieves an instance of Type on itself

I don't understand what you mean with the bolded part.

I have gonne to the web-page, and for the 98 standard, the one before C++11, compilation hangs on work

The output is the same for C++98, C++11 and C++14. This is fundamental stuff, and has not changed.

The altered code you posted does not compile. std::cout is not defined for the custom class, and that's not the correct way to create a new instance of the variable type.

Hello to all my stalkers.

In my knowledge this'd have been equivalent to that code:

int x = 1;
int& y = x; // snatches addreess of x into actual int* y

*y += 1; // does NOT increment the address
std::cout << *y;
*y = 5; // does NOT set the address to 5
printf("%i", *y);

I havent used references for so long. I am still not sure for pre-11 standard???

Since, for example:

int* a;

int** ap=&a;

**ap - provides *a

int x = 1;
int& y = x; // snatches addreess of x into actual int* y
*y += 1; // does NOT increment the address

This does not compile. You do not understand how references work, and you should look them up.


I havent used references for so long. I am still not sure for pre-11 standard???


References in C++ have not ever changed, as far as I know(?). You are just mistaken.
Part of the problem is that the & symbol means two different things:


int a = 5;
int &b = a; //Here, the & symbol means "reference".

int a = 5;
int *b = &a; //Here, the & symbol means "address of".

This is actually somewhat similar to the * symbol:


int *a; //Here, the * symbol means "pointer"

int *a = new int;
*a = 5; //Here, the * symbol means "dereference".

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement