Author | Message | Time |
---|---|---|
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) [/quote] I did the excercise by writing this code: [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; } [/code] Input: [code] Insert an integer: 10 [/code] 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 [/quote] now for my questions... 1.) When compiling I got the warning "10: *pmyint was given a value that was never used." [code] int *pmyint = 0; [/code] 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 [code] int *pmyint; [/code] because I assign the pointer to something before I try to use it? Next... [code] printf("*pmyint\t\t%d\t%d\n", *pmyint, *pmyint+10); [/code] 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. | March 15, 2004, 10:26 PM |
Adron | 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: [code] 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); [/code] [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 [/quote] | March 15, 2004, 10:35 PM |
Eli_1 | 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: [/quote] 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. [/quote] Edit 1: Also, why did you get 0xXXXXXXXX for &i when I got 00XXXXXXXX, just difference in computers? | March 15, 2004, 10:36 PM |
MrRaza | [quote author=Eli_1 link=board=30;threadid=5796;start=0#msg49598 date=1079390200] 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: [/quote] 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. [/quote] Edit 1: Also, why did you get 0xXXXXXXXX for &i when I got 00XXXXXXXX, just difference in computers? [/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. Aswell, check out http://www.hermetic.ch/cfunlib/ast_amp.htm it deals with the use of * and & in C/C++. And, [quote author=Eli_1 link=board=30;threadid=5824;start=0#msg49827 date=1079465531] %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 [/quote] | March 19, 2004, 5:36 AM |
Eli_1 | [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. [/quote] no, I used %p... it was in hex, but it didn't have the 0x infront of it | March 19, 2004, 12:00 PM |
Adron | [quote author=Eli_1 link=board=30;threadid=5796;start=0#msg50379 date=1079697639] [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. [/quote] no, I used %p... it was in hex, but it didn't have the 0x infront of it [/quote] It's implementation specific. On a DOS large memory model compiler, you're likely to get something like 1234:ABCD. | March 19, 2004, 5:04 PM |
vile | And if you don't know what they mean by that, it means segment:offset. | March 22, 2004, 2:58 AM |