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)
:blush:
Printable View
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)
:blush:
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.
The content, as in the value, of X is the address of Y, as you yourself have stated.Quote:
Originally Posted by ulumulu
well and I want from tempLine to get adress of chunchOfData.Code:
char *tempLine;
.......................
tempLine = &chunchOfData[0];
.......................
chunchOfData[i] = '\0';
.....................
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.Quote:
Originally Posted by ulumulu
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...
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?
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. :blush:
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.Quote:
Originally Posted by ulumulu
Yes your are right :)
Cheers.
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;
}
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).Quote:
Originally Posted by olivthill2