0

char a[]="helloworld";

I want to get address of 'l'?

(a+2) or &a[2] gives lloworld. And adding other ampersand shows error.

Then how to get address of 'l'?

If not possible, please explain?

1
  • 3
    I assume you're outputting it: use static_cast<void *>(&a[2]) for an address output. If so, its a possible duplicate of this: stackoverflow.com/questions/10817278/… Commented Jun 1, 2012 at 16:16

5 Answers 5

7

a + 2 is the address of the third element in the array, which is the address of the first l.

In this expression, a, which is of type char[11], is implicitly converted to a pointer to its initial element, yielding a pointer of type char*, to which 2 is added, giving the address of the third element of the array.

&a[2] gives the same result. a[2] is equivalent to *(a + 2), so the full expression is equivalent to &*(a + 2). The & and * cancel each other out, leaving a + 2, which is the same as above.

How this address is interpreted in your program is another matter. You can use it as "a pointer to a single char object," in which case it's just a pointer to the first l. However, you can also interpret it as "a pointer to the C string contained in the array starting at the pointed-to char, in which case you get "lloworld". It depends on how you use the pointer once you get it.

Sign up to request clarification or add additional context in comments.

3 Comments

in C,we can get da address by printf("%d",&a[2]) and 'lloworld' by printf("%s",&a[2])..But since in C++,we have cout only which by default takes string(wat compiler shows)..so how to get address then
@Vj, what makes you think you have only cout in C++? You have printf right there in the cstdio header. And that code isn't getting the address. That code is printing the address. You already get the address with the code you and everyone has shown: a + 2 or &a[2]. Your problem is in printing that value so it looks like an address.
@RobKennedy ya agree wid that... :)
3

Both are correct, but if you want to output it you'll have to either:

  • for C: use an appropriate format string (for C): printf("address: %p\n", &a[2]);

  • for C++: avoid string interpretion by cout by casting to a void*: cout << "address: " << static_cast<void*>(&a[2]) << endl;

3 Comments

-1 the correct print-formatspecifier for an address in C is %p not your %x
@vjshah Why did you tag it as C then?
@user411313, didn't know about %p. Now I do. Good call.
0

I think you want strchr:

char *lAddress = strchr(a, 'l');

This will point to the start of l. You can print this using printf using the %p descriptor.

Comments

0

strchr will return a pointer to (address of) the first occurrence of a given character in a string.

Comments

0

What do you mean it gives up 'lloworld'. If you use it as a C-string it will still be null terminated. Just start a bit further on in the sentence.

2 Comments

in C++,cout<<&a[2] outputs whole string after 2 elements by default..In C,its nt a prblm...So,to get address of particular element in a string in C++,it cant be done by &a[2]...then wats da way?
You have the address - a char *. cout uses this as a string. Do you want the raw address - then cast it to void *.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.