• Welcome to Valhalla Legends Archive.
 

Pointers Addresses and Refrences

Started by Eli_1, March 15, 2004, 04:26 PM

Previous topic - Next topic

Eli_1

I'v been following the, "Teach yourself C/C++ in 21 Days", despite how most people say not to... and I'm on, "Day 9, Refrences"

I finished reading it and started on the excercises (yes I do actually do the excersises at the end of the chapter :P)

Quote
Excersise: Create a program that declares an int, a pointer that points to an int, and a refrence for an int. Display the origional value of an int and use the pointer and refrence to manipulate that value, then display those values.
Extra Credit: Display the address in memory for each of those variables (hint: they should all seem similar :P)

I did the excercise by writing this code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {

char input[128];

int myint = 0;
int *pmyint = 0;
int &rmyint = myint;

   printf("Insert an integer: ");
   fgets(input, 128, stdin);

   myint = atoi(input);
   pmyint = &myint;

   printf("\n\nVar\t\tStart\tNew\n");      
   printf("myint\t\t%d\t%d\n", myint, myint+10);
   printf("*pmyint\t\t%d\t%d\n", *pmyint, *pmyint+10);
   printf("&rmyint\t\t%d\t%d\n\n", rmyint, rmyint+10);
      
   printf("Var\t\tAddress\n");
   printf("This section is not complete\n");

   return 0;
}


Input:

Insert an integer: 10


Output:
Quote
Insert an integer: 10


Var            Start    New
myint        10        20
*pmyint    10        20
rmyint       10        20

Var            Address
This section is not finished

now for my questions...
1.) When compiling I got the warning "10: *pmyint was given a value that was never used."

int *pmyint = 0;

in the chapter on pointers it said to always initialize the pointer to something, if you don't know what to initialize it to, set it to 0 (NULL pointer). this is because pointers that are never initialized can be very dangerous (they are called wild pointers).

should I change it to
int *pmyint;

because I assign the pointer to something before I try to use it?

Next...

printf("*pmyint\t\t%d\t%d\n", *pmyint, *pmyint+10);

2.) the * before pmyint gives me the value of pmyint, and without the * would give me the address in memory, right?

3.) when I use fgets() and compare that to something, say "/exit", the function never returns 0, even if I do input "/exit"...
why does this happen?

4.) how would I display the address in memory of the 3 variables useing printf()?

hmm, like how you can do %d, and %s, and %c, what would I use?

[Edit 1] my info. on wild pointers was wrong, fixed it.

Adron

#1
1. You could initialize it to &myint instead
2. The * before pmyint gives you the value pointed to. *pmyint = value pointed to; pmyint = address value; &pmyint = address of pmyint
3. fgets will read a '\n' at the end of the input - the carriage return you typed. Either strip that away or include that in what you look for.
4. You can display the address using %d or %x, since it's actually just a number, but I think %p is the correct way of doing it.


edit:
Also, when the book says to use the pointer and reference to manipulate the value, I think they mean something like this:


int i = 47;
int *ip = &i;
int &ir = i;
printf("i=%d  *ip=%d  ir=%d\n", i, *ip, ir);
*ip += 15;
printf("i=%d  *ip=%d  ir=%d\n", i, *ip, ir);
ir += 15;
printf("i=%d  *ip=%d  ir=%d\n", i, *ip, ir);

printf("&ip=%p  ip=%p  *ip=%d\n", &ip, ip, *ip);
printf("&ir=%p  ir=%d\n", &ir, ir);
printf("&i=%p  i=%d\n", &i, i);


Quote
i=47  *ip=47  ir=47
i=62  *ip=62  ir=62
i=77  *ip=77  ir=77
&ip=0xbffff500  ip=0xbffff504  *ip=77
&ir=0xbffff504  ir=77
&i=0xbffff504  i=77


Eli_1

#2
thanks, Adron  :D

Quote
edit:
Also, when the book says to use the pointer and reference to manipulate the value, I think they mean something like this:

You where right, when I wrote this I wrote the excercise from memory, and beefed up the excercise so I could make sure I understood what I was doing  :-\. I re-read it and the actual line in the chapter about minipulating the variables is:
Quote
Use the pointer and the reference to manipulate the value in the int.

Edit 1:
Also, why did you get 0xXXXXXXXX for &i when I got 00XXXXXXXX, just difference in computers?

MrRaza

#3
Quote from: Eli_1 on March 15, 2004, 04:36 PM
thanks, Adron  :D

Quote
edit:
Also, when the book says to use the pointer and reference to manipulate the value, I think they mean something like this:

You where right, when I wrote this I wrote the excercise from memory, and beefed up the excercise so I could make sure I understood what I was doing  :-\. I re-read it and the actual line in the chapter about minipulating the variables is:
Quote
Use the pointer and the reference to manipulate the value in the int.

Edit 1:
Also, why did you get 0xXXXXXXXX for &i when I got 00XXXXXXXX, just difference in computers?


Notice how in Adron's code he use's %p, which is used to return a pointers address? Anyway, you used %d which would print out some value in decimal form.

Aswell, check out http://www.hermetic.ch/cfunlib/ast_amp.htm it deals with the use of * and & in C/C++.

And,
Quote from: Eli_1 on March 16, 2004, 01:32 PM

%c Character 'a'
%d or %i -- Signed decimal integer 392
%e Scientific notation (mantise/exponent) using e character 3.9265e2
%f Decimal floating point 392.65
%g Use shorter %e or %f 392.65
%o Signed octal 610
%s String of characters sample
%u Unsigned decimal integer 7235
%x Unsigned hexadecimal integer 7fa
%p Address pointed by the argument B800:0000

Eli_1

#4
Quote
Notice how in Adron's code he use's %p, which is used to return a pointers address? Anyway, you used %d which would print out some value in decimal form.

no, I used %p... it was in hex, but it didn't have the 0x infront of it

Adron

Quote from: Eli_1 on March 19, 2004, 06:00 AM
Quote
Notice how in Adron's code he use's %p, which is used to return a pointers address? Anyway, you used %d which would print out some value in decimal form.

no, I used %p... it was in hex, but it didn't have the 0x infront of it

It's implementation specific. On a DOS large memory model compiler, you're likely to get something like 1234:ABCD.

vile

And if you don't know what they mean by that, it means
segment:offset.