-
June 19th, 2009, 03:09 AM
#1
[RESOLVED] Pointers (confused)
Good day.
Probably the foolish question ever, but still.
Lets say I have pointer X*and hold in it address of &Y.
How do I get the the address of Y from X. (Not the X address but Y from X)
Share and always try to give back more.
-
June 19th, 2009, 03:52 AM
#2
Re: Pointers (confused)
Originally Posted by ulumulu
Good day.
Probably the foolish question ever, but still.
Lets say I have pointer X*and hold in it address of &Y.
How do I get the the address of Y from X. (Not the X address but Y from X)
Code:
int Y = 0;
int* X = &Y;
std::cout << &(*X) << std::endl;
-
June 19th, 2009, 03:58 AM
#3
Re: Pointers (confused)
Wait. What is the address of &Y? &Y may not have an address, as I understand. If you want the address of Y given X, then the answer is the value of X, by definition of pointer.
-
June 19th, 2009, 03:58 AM
#4
Re: Pointers (confused)
Originally Posted by Mybowlcut
Code:
int Y = 0;
int* X = &Y;
std::cout << &(*X) << std::endl;
Oh ok. I did that and It still shows the content of X. Probably I did a mistake somewhere else. Most likely with Y in uper code. Anyway thankyou.
Share and always try to give back more.
-
June 19th, 2009, 04:00 AM
#5
Re: Pointers (confused)
Originally Posted by ulumulu
It still shows the content of X.
The content, as in the value, of X is the address of Y, as you yourself have stated.
-
June 19th, 2009, 04:02 AM
#6
Re: Pointers (confused)
Code:
char *tempLine;
.......................
tempLine = &chunchOfData[0];
.......................
chunchOfData[i] = '\0';
.....................
well and I want from tempLine to get adress of chunchOfData.
Share and always try to give back more.
-
June 19th, 2009, 04:06 AM
#7
Re: Pointers (confused)
Originally Posted by ulumulu
I want from tempLine to get adress of chunchOfData.
Why do you need the address of chunchOfData? That address should coincide with the address of the first element of chunchOfData, which is what tempLine contains, but I would like to know the context of the problem before suggesting a solution.
-
June 19th, 2009, 04:10 AM
#8
Re: Pointers (confused)
Ok the problem is:
I have some text file. And I am parsing some data of it. So then I found the items i need. I want to hold pointers of it in vector that I could use it many times. That vector with pointers will be used in some different ways...
Last edited by ulumulu; June 19th, 2009 at 04:13 AM.
Share and always try to give back more.
-
June 19th, 2009, 04:14 AM
#9
Re: Pointers (confused)
I do not see how that links to chunchOfData. Are you trying to store pointers to individual characters in a C-style string? If so, what exactly is the problem?
-
June 19th, 2009, 04:22 AM
#10
Re: Pointers (confused)
well chunchofData is actualy a small pease off data (block) which is parsed at the moment.
Anyway thanks for your help LaserLight. I think I found the problem.
Problem: knowledge. I am still learning so I am doing a lot mistakes. And I am getting data address , but output shows the content of this address. so there is no problem in code, only in knowledge.
Share and always try to give back more.
-
June 19th, 2009, 04:24 AM
#11
Re: Pointers (confused)
Originally Posted by ulumulu
And I am getting data address , but output shows the content of this address. so there is no problem in code, only in knowledge.
Oh, as in you tried to print the pointer to char, but ended up printing the associated string instead of a numeric address? A simple solution would be to cast the pointer to char to be a pointer to void.
-
June 19th, 2009, 04:28 AM
#12
Re: Pointers (confused)
Yes your are right
Cheers.
Share and always try to give back more.
-
June 19th, 2009, 04:34 AM
#13
Re: Pointers (confused)
1. Do you learn C++ or C?
If you program in C++, you will rather use "strings" than "arrays of charaters". Therefore, you will almost never use char foo[10]; which is for an array of characters.
If you program in C, you will use arrays of characters.
2. Here is an example showing you that, in the case of an array of characters, you have three ways to have its address, foo, &foo[0], and &foo. But usually, you use the first method
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
// Declarations
char a;
char *a_ptr;
char s[5];
char *s_ptr1, *s_ptr2, *s_ptr3;
// Initializations
a = 'A';
a_ptr = &a;
strcpy(s, "abcd");
s_ptr1 = s;
s_ptr2 = &s[0];
s_ptr3 = (char *)&s;
// Display content
printf("a=%c, *a_ptr=%c, a_ptr=%ld.\n", a, *a_ptr, (long int) a_ptr);
// a=A, *a_ptr=A, a_ptr=7601647.
printf("s=%s, s_ptr1=%ld, s_ptr2=%ld, s_ptr3=%ld.\n", s,
(long int) s_ptr1, (long int) s_ptr2, (long int) s_ptr3);
// s=abcd, s_ptr1=7601616, s_ptr2=7601616, s_ptr3=7601616.
system("PAUSE");
return 0;
}
-
June 19th, 2009, 04:41 AM
#14
Re: [RESOLVED] Pointers (confused)
Originally Posted by olivthill2
2. Here is an example showing you that, in the case of an array of characters, you have three ways to have its address, foo, &foo[0], and &foo. But usually, you use the first method
Only the last would result in a pointer to the array. The other two would result in a pointer to the first element of the array (except in the case of sizeof(foo), in which case the expression is not converted to a pointer).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|