1

So I need to have a pointer to a value in a const char array. But I can't quite get it to work without errors. Here's the code.

int main (void)
{
    const char *alp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    char *ptr = &alp[3];

    printf("%s\n", ptr);

    return 0;
}

Edit- Sorry for not mentioning the errors. The thing is I get tons of different errors depending on where I put different asterisks and ampersands. There is no one particular error. One of the more frequent ones I get says "incompatible integer to pointer conversion assigning to 'char *' from 'const char';"

In the end I just want "ptr" to be equal to a pointer pointing to "D" in the array "alp".

5
  • 1
    what is it that you are not getting right? Commented Jun 7, 2013 at 19:59
  • 1
    Questions often come with question marks. What is your question? "What's wrong with my code?" isn't a valid question on SO. Commented Jun 7, 2013 at 19:59
  • 2
    What are the errors you are seeing? Can you not compile? Are they runtime errors, does it crash? What output do you expect? Commented Jun 7, 2013 at 19:59
  • Are you getting errors or warnings? Commented Jun 7, 2013 at 20:04
  • You are trying to assign a pointer to a const char to a non-const char pointer. I'm guessing the compiler is complaining about that. Commented Jun 7, 2013 at 21:21

3 Answers 3

4

If you only want one character to print, change the %s to %c and dereference the pointer

printf("%c\n", *ptr);

It's true that you had a character pointer but %s tells printf to print from that pointer until it reads a null character. So we switch to %c which will print one character but it expects a value rather than a pointer to a value.

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

1 Comment

What do you mean to dereference the pointer, and which pointer? If I change to %c I get an ERROR "assigning to 'char *' from 'const char *' discards qualifiers" That's with the code >>char *ptr; ptr = &alp[3]<<
2

alp is a pointer to constant char.

ptr is a pointer to a non-const char.

If you want that to work out you would need to change ptr to be defined as:

char const * ptr = &alp[3];

1 Comment

Thank you, this worked! I can't believe I didn't think to make it a const char. Anyways thank you!
0

The const is saying that you're not going to change it, but then you're trying to copy the reference to a variable without that limitation. Instead, try:

const char *ptr = &alp[3];

But note that as other posters have already suggested, this code will give you a pointer to a string beginning with the D (e.g. DEFGHI...), not just the character D.

3 Comments

A pointer to a char and a pointer to a string (0-terminated sequence of chars) is exactly the same in C.
Yes I understand that - the question asks for a pointer to the D, and I am not sure if that means they want the specific character D, or the string from that point onwards.
But there is no difference. If you do puts(ptr); the pointer will be treated as a string. With printf("%c\n", *ptr);, the same pointer will act as a pointer to a single char. It's the program logic which makes the difference.

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.