9 Replies - 640 Views - Last Post: 25 July 2010 - 10:16 AM Rate Topic: -----

#1 Guest_Flustered*


Reputation:

Pointers Help

Posted 24 July 2010 - 03:57 PM

In class Thursday, we started pointers. That night, a simple homework assignment. Show how you would change the value of a pointer with a function outside of main.

I turned in this code.

#include <iostream>
using namespace std;

int adder(int temp);

int main()
{
// beginning values 
int n=6;
int *num=&n; 

// printout values
cout << "In main, we start with these values" << endl;
cout << "value of n - " << n << endl;
cout << "Value of *num - " << *num << endl; 
cout << "Value of &num -" << &num << endl;    

//jump to adder function
*num=adder(*num);

// printout new values
cout << "\n\nBack in main we get this result." << endl;
cout << "value of n - " << n << endl;
cout << "Value of *num - "<< *num << endl; 
cout << "Value of &num -"<< &num << endl;

    
    cout << "\n\n" << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

int adder(int temp){
     temp++;
     cout << "\n\nIn the 'adder' function we change *num to " << temp << endl;
     return (temp);     
     } 



Friday, he gave me a "C", and said he would not accept my 4 apologies. I must have given him the dear in the headlights look because, before I could say anything else he said "Comments are nothing more than an apology... figure it out." I figure he must like me, 67% of the class failed.


First, Is there anything wrong with the code?
Second, Any notion to his apology remark?

Edited by macosxnerd101: Title renamed to be more descriptive. :)

Is This A Good Question/Topic? 0

Replies To: Pointers Help

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,563
  • Joined: 09-June 09

Re: Pointers Help

Posted 24 July 2010 - 04:03 PM

Does this function change the value of any pointer?
int adder(int temp){
	temp++;
	cout << "\n\nIn the 'adder' function we change *num to " << temp << endl;
	return (temp);     
} 



Ill tell you what this function does
int adder(int temp) //creates a "new" variable with a "new memory location named temp, then the parameter you pass in it copied to temp
{
	temp++; //increments the new variable, 
	cout << "\n\nIn the 'adder' function we change *num to " << temp << endl;
	return (temp);  //now returns the new variable   
} 



What you need to do is pass in a pointer and change the value at that memory location rather than creating a new variable


Hers a function that will show the value at that address, maybe that will help you get going
void displayPointer(int *temp) 
{
	cout <<"The value at memory location "<<temp<<" is "<<*temp<<endl;
}


This post has been edited by ImaSexy: 24 July 2010 - 04:06 PM

Was This Post Helpful? 0
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Pointers Help

Posted 24 July 2010 - 05:11 PM

ok, now i've got a question on this. I was running this just to see what I could come up with and when I did

void adder(int* temp){
    *temp += 1; // also works with *temp = *temp + 1;
    cout << "\n\nIn the 'adder' function we change *num to " << *temp << endl;    
} 



I got the desired 7 both in the function and back in main after I returned from the function. However, when I did this

void adder(int* temp){
    *temp++;
    cout << "\n\nIn the 'adder' function we change *num to " << *temp << endl;    
} 



the value of temp inside the functions was 134515024 and back in main it was still 6. I had always been told that ++ and +=1 were the same thing but there must be some difference. I'm using gcc on a linux box.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,563
  • Joined: 09-June 09

Re: Pointers Help

Posted 24 July 2010 - 05:18 PM

take a look


(*temp)++ is equivelent to *temp = *temp +1;

when you do *(temp++), you are actually incrementing your pointer location
	const int SIZE = 5;
	int myArray[SIZE] = {1,2,3,4,5};
	int *ptr = &myArray[0];
	for(int i=0; i<SIZE; i++)
		cout<<*ptr++<<endl;


This post has been edited by ImaSexy: 24 July 2010 - 05:24 PM

Was This Post Helpful? 0
  • +
  • -

#5 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Pointers Help

Posted 24 July 2010 - 05:31 PM

ah ok, so we need to actually get the value that's being pointed to before we apply the ++.

But we don't need the () if we're doing ++*temp?
Was This Post Helpful? 0
  • +
  • -

#6 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Pointers Help

Posted 24 July 2010 - 05:41 PM

didn't mean to hijack this thread but maybe it'll help them learn a little bit more about it too
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Flustered*


Reputation:

Re: Pointers Help

Posted 24 July 2010 - 06:11 PM

View PostImaSexy, on 24 July 2010 - 03:03 PM, said:

Does this function change the value of any pointer?
int adder(int temp){
	temp++;
	cout << "\n\nIn the 'adder' function we change *num to " << temp << endl;
	return (temp);     
} 



Ill tell you what this function does
int adder(int temp) //creates a "new" variable with a "new memory location named temp, then the parameter you pass in it copied to temp
{
	temp++; //increments the new variable, 
	cout << "\n\nIn the 'adder' function we change *num to " << temp << endl;
	return (temp);  //now returns the new variable   
} 



Look at the call... *num=adder(*num);

I guess it's not the same?? The pointer was changed, maybe that's why I got a grade. LOL
Ok. Thank you.

What about the comments are apologies think... any clue?
Was This Post Helpful? 0

#8 theRedNeck  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-February 10

Re: Pointers Help

Posted 24 July 2010 - 06:25 PM

View PostImaSexy, on 24 July 2010 - 03:03 PM, said:

Heres a function that will show the value at that address, maybe that will help you get going
void displayPointer(int *temp) 
{
	cout <<"The value at memory location "<<temp<<" is "<<*temp<<endl;
}



I'm not real big on pointers, but um.... Shouldn't that be *&temp???

cout <<"The value at memory location "<<temp<<" is "<<*temp<<endl;
gives me "invalid type argument of unary" error. Maybe it's just my compiler. (Dev c++)

As far as the apology comment.
Looking at Richard Bucklands lectures on Youtube, he made mention that some super guru programmer once said "Comments in your code are an apology, because it means your code is not clear enough to understand without the comments." Or something to that effect. Basically, good codes have comments. Great codes don't need them.

This post has been edited by theRedNeck: 24 July 2010 - 06:30 PM

Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,563
  • Joined: 09-June 09

Re: Pointers Help

Posted 24 July 2010 - 06:25 PM

Quote

A comment is an apology for not choosing a more clear name, or a more reasonable set of parameters, or for the failure to use explanatory variables and explanatory functions. Apologies for making the code unmaintainable, apologies for not using well-known algorithms, apologies for writing 'clever' code, apologies for not having a good version control system, apologies for not having finished the job of writing the code, or for leaving vulnerabilities or flaws in the code, apologies for hand-optimizing C code in ugly ways. And documentation comments are no better. In fact, I have my doubts about docstrings

Was This Post Helpful? 1
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: Pointers Help

Posted 25 July 2010 - 10:16 AM

If you're supposed to be changing a value in a function with a pointer, then this is a total fail.
int adder(int temp);
//...
*num=adder(*num);



Since you've done nothing with pointers, the C was a gift.

This is probably what was expected:
#include <iostream>

using namespace std;

void adder(int *n) { *n = *n + 1; }

int main() {
	int n=6;
	
	cout << "value of n - " << n << endl;
	adder(&n);
	cout << "value of n - " << n << endl;
	return 0;
	
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1