Thread: character pointer question (should be easy, but I'm new to this)

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    11

    character pointer question (should be easy, but I'm new to this)

    I have a character pointer question.

    Given a char pointer called ptr (i.e. char *ptr) and the alphabet array (const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), how do I make ptr point to the letter 'D' in the alphabet array (i.e. how to set ptr to memory address where 'D' is stored)?

    The constant alphabet array I initialized to the string below looks like this (as shown above):

    const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    I assume that this is an easy question, but I am still a little lost at the moment.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How would you get ptr to point to alphabet[0]?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    11
    Oops...

    const char alphabet [26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Um, please answer my question. In this case, specifying the 26 makes no difference.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    While we wait for an answer to the question laserlight asked, I would like to point out one thing.

    Putting the alphabet in double quotes makes it a string constant - meaning that the compiler will automatically append the string with a null character (\0). Therefore, the array size would need to be the number of characters in the string plus one for the null character.

    Code:
    char alphabet[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    You could get away with using 26 if you didn't want a string, but simply an array of chars. In that case, you would have to define each array element independently; something like:

    Code:
    char alphabet[26] = {'A','B','C', ... 'Z'};
    /* just an example; each element would have to be assigned, I only used the
    ellipsis because I didn't feel like typing out each letter */

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Matticus
    You could get away with using 26 if you didn't want a string, but simply an array of chars. In that case, you would have to define each array element independently; something like:
    No, Dynesclan's line of code in post #3 initialises the array named alphabet such that it does not contain a null terminated string: the null character from the string literal is just ignored. So, you don't need to "define each array element independently", unless you want to be compatible with C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I stand corrected. Thank you.

    [edit] Considering the level of the OPs experience, based on previous posts, I thought this advice might help future learning pitfalls in using strings
    Last edited by Matticus; 06-10-2012 at 03:46 AM.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    11
    Sorry to keep you waiting for an answer. Honestly, I'm not sure how to set ptr to point to an individual letter in the array. At first I thought it would have to do with converting the letter to their ASCII values, but dismissed the idea because I thought it didn't relate to the memory addresses.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, you would take the address of that character, so it could be:
    Code:
    ptr = &alphabet[0];
    Alternatively, since an array is converted to a pointer to its first element, you could write:
    Code:
    ptr = alphabet;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    11
    Are you saying that the [0] points to 'A', at the start of the array? Does that mean that since 'D' is 3 characters away from 'A', the ptr would use [3]?

    For example:
    ptr = &alphabet [3];

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dynesclan
    Are you saying that the [0] points to 'A', at the start of the array?
    Not quite: the index of the first element of the array is 0.

    Quote Originally Posted by Dynesclan
    Does that mean that since 'D' is 3 characters away from 'A', the ptr would use [3]?
    The index of the element of the array with the value of 'D' is 3.

    This distinction is necessary when you see that I use the address of operator & to get the pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  2. Easy Pointer question
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-30-2008, 08:21 PM
  3. Easy Question -> File Pointer
    By Marc in forum Windows Programming
    Replies: 2
    Last Post: 11-19-2005, 09:57 AM
  4. character pointer question
    By osmosis in forum C Programming
    Replies: 3
    Last Post: 07-11-2003, 07:46 AM
  5. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM