C++ Pointers: Solving "g is not defined" & "return" Meaning

  • C/C++
  • Thread starter beatlesben
  • Start date
  • Tags
    C++
In summary, when I ran the code, it said that g wasn't defined, but when I entered in the value of g it worked perfectly.
  • #1
beatlesben
4
0
Hello, I'm teaching myself C++. I'm pretty much at level one. Anyway I was trying to make a code, playing with pointers:

#include <iostream>

using namespace std;

int main()
{
int x;
int g;
int *p;
x = 1 + g;
cin>> g;
p = &x;
cin.ignore();
cout<< *p <<"\n";
cin.get();
return 1;
}


When I run the code it says that g is not defined, but g is what you have to enter, so I don't understand how to define it.

Also could someone explain "return." I don't understand what the value next to it means.

Thanks,
Remember, this is my second day of learning this...
 
Technology news on Phys.org
  • #2
beatlesben said:
Hello, I'm teaching myself C++. I'm pretty much at level one. Anyway I was trying to make a code, playing with pointers:

#include <iostream>

using namespace std;

int main()
{
int x;
int g;
int *p;
x = 1 + g;
cin>> g;
p = &x;
cin.ignore();
cout<< *p <<"\n";
cin.get();
return 1;
}


When I run the code it says that g is not defined, but g is what you have to enter, so I don't understand how to define it.

Also could someone explain "return." I don't understand what the value next to it means.

Thanks,
Remember, this is my second day of learning this...

Hi Beatlesben,

You've properly declared the variable g as integer, so that's set aside a piece of memory for the variable g. However, you have not specified the value of g until:

cin>> g;

When you get to the line of code that says:

x = 1 + g;

C++ does not have a value for g at this time. To fix this, make sure to put the

cin>> g;

before the:

x = 1 + g;

This way, you'll properly read in the value and place it into the variable g, and then you can use g for any future calculations.


----

As a side note, you're already teaching yourself pointers and address references? You must be a fast learner.
 
  • #3
You're assigning x to 1 + g before g has been read.

Also return 1 typically means failure while 0 is success. A better approach is to #include <cstdlib> and return EXIT_SUCCESS
 
Last edited:
  • #4
Thanks for the quick responses.
It worked perfectly!
 
  • #5


Hello! Congrats on teaching yourself C++! Pointers can be tricky to understand at first, so don't worry if you're having trouble with them.

In your code, you have declared two variables: x and g. However, you have not given a value to g before trying to use it in your code. This is why you are getting the error "g is not defined". To fix this, you need to ask the user to input a value for g before using it in your code. You can do this by using the cin function, as you have done with x. So your code should look something like this:

#include <iostream>

using namespace std;

int main()
{
int x;
int g;
int *p;
cout << "Please enter a value for g: ";
cin >> g;
x = 1 + g;
p = &x;
cout << *p << "\n";
cin.ignore();
cin.get();
return 1;
}

Now, when you run the code, it will ask for a value for g before using it in the calculation.

As for the "return" statement, it is used to indicate the end of a function. In your code, the main function is declared as an int (integer) function, which means it is expected to return an integer value. In this case, the value 1 is being returned. This value is typically used to indicate if the program ran successfully (1) or encountered an error (0). So in your code, the return statement is just indicating that the main function has ended and the program can terminate.

I hope this helps clarify things for you. Keep up the good work with learning C++!
 

1. What is a pointer in C++?

A pointer in C++ is a variable that stores the memory address of another variable. It allows for direct access and manipulation of data stored in memory, instead of just the value of the data.

2. How do I solve the error "g is not defined" when using pointers?

This error typically occurs when a pointer is declared but not initialized with a valid memory address. To solve this error, make sure to initialize the pointer with the address of a variable or use the nullptr keyword to indicate that the pointer points to nothing.

3. What does the "return" keyword mean when using pointers?

The "return" keyword in C++ is used to end a function and return a value. When used with pointers, it allows the function to return the value stored at the memory address pointed to by the pointer.

4. How do I access the value stored at a specific memory address using pointers?

To access the value stored at a specific memory address, you can use the dereferencing operator (*) before the pointer variable. This will give you access to the value stored at the memory address pointed to by the pointer.

5. Can pointers be used to allocate memory dynamically in C++?

Yes, pointers can be used to allocate memory dynamically in C++. This allows for the creation of data structures such as linked lists and trees, as well as efficient memory management in programs.

Similar threads

  • Programming and Computer Science
Replies
3
Views
727
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
1
Views
987
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
4
Replies
118
Views
6K
Back
Top