Thread: Some C Pointer Questions

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    15

    Some C Pointer Questions

    (1) Declare a char array called alpha, and initialize it to the string “ABCDEFGHIJKLMNOPQRSTUVWXYZ”. In your declaration, make the alphabet array read-only, so none of the letters in the array can be changed later in the program.

    const char alpha[] = "ABCDEFGHIJKLMOPQRSTUVWXYZ";

    (is 'const' what makes the array read-only?)

    ------------------------------------------------------------------
    (2) Given a char pointer called ptr (i.e. char *ptr) and the alpha array declared in problem (1) above, make ptr point to the letter ‘D’ in the alpha array (i.e. set ptr to the memory address where ‘D’ is stored).

    This one I have issues with a lot. I am assuming something like:

    prt = *(alpha+4);

    ------------------------------------------------------------------
    (3) What’s wrong with the code below? (Circle the line with the error, and describe the error):

    Code:
    int *int_ptr;
    int primes[ ] = { 2,3,5,7 };
    int composite[ ] = {4,6,8,9};
    int_ptr = primes;
    composite = int_ptr;  //This is the wrong line, I think
    
    ^^I am not sure exactly how to explain that the last line is the wrong code.

    ------------------------------------------------------------------
    (4) Pointers enable passing variables to functions by: (Answer in bold)
    A. Pass by value
    B. Pass by reference
    C. Pass by number
    D. Pass by operator

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joshlete
    (is 'const' what makes the array read-only?)
    Yes, or more precisely it declares that the elements of the array are read-only because each of them is of type const char.

    Quote Originally Posted by joshlete
    (2) Given a char pointer called ptr (i.e. char *ptr) and the alpha array declared in problem (1) above, make ptr point to the letter ‘D’ in the alpha array (i.e. set ptr to the memory address where ‘D’ is stored).
    This question is wrong in its parenthesized premise: ptr should be a const char*, not a char*, since alpha is an array of const char.

    Quote Originally Posted by joshlete
    This one I have issues with a lot. I am assuming something like:

    prt = *(alpha+4);
    Close, but since alpha is an array, it is converted to a pointer to its first element. Therefore, alpha + 4 is also a pointer. But once you dereference it, you get what the pointer points to, whereas you want the pointer.

    Quote Originally Posted by joshlete
    ^^I am not sure exactly how to explain that the last line is the wrong code.
    Recall that you cannot assign to an array, i.e., explain why and perhaps provide the likely expected code.
    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 2017
    Posts
    15
    Quote Originally Posted by laserlight View Post
    This question is wrong in its parenthesized premise: ptr should be a const char*, not a char*, since alpha is an array of const char.
    Since char *prt is just pointing to a location, does it really matter unless you try modifying it? Writing up the code to test it still gives the right result, I think.

    Quote Originally Posted by laserlight View Post
    Close, but since alpha is an array, it is converted to a pointer to its first element. Therefore, alpha + 4 is also a pointer. But once you dereference it, you get what the pointer points to, whereas you want the pointer.
    Oh right, that makes sense. I kept assuming it wanted a dereference. Thank you!


    Quote Originally Posted by laserlight View Post
    Recall that you cannot assign to an array, i.e., explain why and perhaps provide the likely expected code.
    So basically it wouldn't work because int_prt is pointing to the beginning of the array. So its trying to assign composite to the beginning of the array, instead of the array itself?

    So then int_prt = primes; is fine since int_prt is a pointer to a integer, which is being assigned to primes[] array?

  4. #4
    Registered User
    Join Date
    May 2017
    Posts
    15
    Quote Originally Posted by laserlight View Post
    This question is wrong in its parenthesized premise: ptr should be a const char*, not a char*, since alpha is an array of const char.
    Does it matter though? Since char *prt is a pointer pointing to the beginning of the alpha array, which is already defined as constant.

    Quote Originally Posted by laserlight View Post
    Close, but since alpha is an array, it is converted to a pointer to its first element. Therefore, alpha + 4 is also a pointer. But once you dereference it, you get what the pointer points to, whereas you want the pointer.
    I see. So then its just "prt = (alpha+3);


    Quote Originally Posted by laserlight View Post
    Recall that you cannot assign to an array, i.e., explain why and perhaps provide the likely expected code.
    So that last code doesn't work because int_prt is pointing to the beginning of the primes[] array. And it is trying to assign the pointer to composite. So its trying to assign a pointer to the beginning of an array to another array. Boy that hurts my mind to try and visualize.

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    15
    Also in question 4. The answer is B, right? I know it can't be A or C, but possibly maybe it's D.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Silly pointer questions
    By sillyquestion4u in forum C Programming
    Replies: 4
    Last Post: 06-20-2015, 08:41 PM
  2. Pointer questions
    By johnpuppa in forum C Programming
    Replies: 16
    Last Post: 07-29-2014, 02:19 PM
  3. Pointer Questions
    By tgrieger in forum C Programming
    Replies: 4
    Last Post: 04-09-2011, 10:14 PM
  4. Two Pointer Questions
    By chris.r in forum C Programming
    Replies: 13
    Last Post: 06-04-2010, 09:40 PM
  5. Pointer questions
    By 1rwhites in forum C Programming
    Replies: 1
    Last Post: 10-20-2005, 09:55 AM

Tags for this Thread