0

I am new to C++ and trying to convert string into integer. I was using atoi but there are some restrictions so I start using strtol which works perfectly. However, I would like to learn more on *temp and &temp (I have google and learn that it is a temporary space for storage) but would like to learn the difference and when to use which.

char *temp;
int m = strtol (argv[1],&temp,10);
if (*temp != '\0')
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
jessycaaaa
  • 55
  • 1
  • 6
  • 1
    `&temp` = take the address of `temp` and create a pointer. `*temp` = take where the temp pointer points to. Suggestion: forget all this and use smart pointers and STL. What you have googled focused on the "temp" as a name, not the & and * operators. – Michael Chourdakis Feb 09 '20 at 09:23
  • 2
    Maybe you want [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? Even most online tutorials should cover the address-of-operator (&) and value-of-operator (*). Also be aware, that your piece of code is essentially C, not C++. – Lukas-T Feb 09 '20 at 09:28
  • If `temp` is you, `&temp` is where you live. In the other direction, if `temp` is where you live, `*temp` is you. – molbdnilo Feb 09 '20 at 09:37

3 Answers3

0

*temp is a pointer to a variable named temp and &temp takes the address of that variable

PurpleKiwi
  • 90
  • 7
0

First of all jessycaaaa welcome to Stackoverflow.

I am new to C++ and trying to convert string into integer.

For me this looks like plain C-code. You can compile this with a C++ compiler though.

I was using atoi but there are some restrictions so I start using strtol which works perfectly.

Since you get an undefined behavior using atoi when argv[1] contains something different than a number, strtol is an approach to go for. If you share us a bit more code, we would help you better on your questions.

However, I would like to learn more on *temp and &temp (I have google and learn that it is a temporary space for storage) but would like to learn the difference and when to use which.

First of all you have to distinguish between use and declaration

char *temp;

Here you declare (*-symbol in declaration) a pointer named temp of type char. A pointer is a variable which stores the memory address (where it is pointing to). Here you did not define an address so it most likely will point a random space, but then

int m = strtol (argv[1],&temp,10);

you pass the address of the pointer (&-symbol, use-case, address-of operator) to strtol, so you get an address pointing to the part of the argv[1] where the number literals end, that is all fine. The function also returns the numerical value of the read string as long and is converted to an int.

if (*temp != '\0')

Here you access the value of what the address is pointing to (*-symbol, use-case, dereference operator). \0 is normally set as indication for a null-terminated string. So you are asking if the previously read end part has the null-termination character.

You know what: in C++ there are more elegant ways to accomplish that using stringstreams:

std::stringstream

Just an idea if you don't want to handle too much string manipulation in C and annoyances with pointers.

Also I would read a good book about C (not C++). C++ has also the references don't get confused by those. If you dominate the pointer-concept of C, I'm pretty sure everything else will be very clear for you.

Best regards

Gerhard Stein
  • 1,543
  • 13
  • 25
0

* and & are one of the first hurdles that programmers new to C and C++ have to take.

To really understand these concepts, it helps to know a bit more about how memory works in these languages.

First of all: C++ is just C but with classes and many other additional features. Almost all C programs are valid C++ programs. C++ even started out as a language that was compiled to C first.

Memory is, roughly speaking, divided in two parts, a 'stack' and a 'heap'. There are also other places for the code itself and compile-time constants (and maybe a few more) et cetera but that doesn't matter for now. Variables declared within a function always live on the stack. Let's see this in action with a simple example and analyse how memory is organized to build a mental model.

#include <iostream>

void MyFunction() {
    int intOnStack = 5;
    int* intPtrOnStack = new int(6); // This int pointer points to an int on the heap
    std::cout << intOnStack << *intPtrOnStack;
    delete intPtrOnStack;
}

int main() { MyFunction(); }

This program prints 56 when executed. So what happens when MyFunction() gets called? First, a part of the stack is reserved for this function to work with. When the variable intOnStack is declared within the function, it is placed in this part of the stack and it is initialized with (filled with) the int value 5.

Next, the variable intPtrOnStack is declared. intPtrOnStack is of type int*. int*'s point to int's by containing their memory-address. So an int* is placed on the stack and it is initialized with the value that results from the expression new int(6). This expression creates a new int on the heap and returns the memory-address of this int (an int*) to it. So that means that intPtrOnStack now points to the int on the heap. Though the pointer itself lives on the stack.

The heap is a part of memory that is 'shared' by all functions and objects within the program. The stack isn't. Every function has its own part of the stack and when the function ends, its part of the stack is deallocated.

So int*'s are just memory-addresses of int's. It doesn't matter where the int lives. int*'s can also point to int's on the stack:

#include <iostream>

void MyFunction() {
    int intOnStack = 5;
    int* intPtrOnStack = &intOnStack; // This int pointer points to intOnStack
    std::cout << intOnStack << *intPtrOnStack;
}

int main() { MyFunction(); }

This prints 55. In this example we also see the &-operator in action (there are several uses of & like the bit-wise-and, I'm not going into them).

& simply returns the memory-address (a pointer!) of its operand. In this case its operand is intOnStack so it returns its memory-address and assigns it to intPtrOnStack.

So far, we've seen only int* as types of pointers but there exist pointer-types for each type of object that has a memory-address, including pointers. That means that a thing like int** exists and simply means 'pointer to a pointer to an int'. How would you get one? Like this: &intPtrOnStack.

Can pointers only live on the stack? No: new int*(&intPtrOnStack). Or new int*(new int(5)).

Jupiter
  • 1,421
  • 2
  • 12
  • 31
  • 1
    "C++ is just C with a 'few' extra things." I am sorry to say, but no. C and C++ share some common base, but they do differ in a lot of cases. It's dangerous to say C++ is C with extra things, because then you will expect C code to work the same way in C++. And it doesn't. – bolov Feb 09 '20 at 10:57
  • That is why I put quotes around 'few' but apparently this didn't put enough emphasis on how big the difference is. I'll rephrase – Jupiter Feb 09 '20 at 10:58