Thread: A question on pointers

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    140

    A question on pointers

    Hi all.

    I have two questions.

    1) Please take a look at the following code:

    Code:
    char test[] = "hi all";
    char *pt = test;
    This bit of code makes perfect sence, since test == &test[0]. Now look at

    Code:
    char *test = "hi all";
    Now this bit I cannot wrap my head around. Where is the logic in this statement? Is the string "hi all" an address?


    2) Why is it not possible in C to use

    Code:
    int *test = {1,2,3};

    Thank you very much in advance.

    Best regards,
    Niles.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    So in this case "hi all" is a string terminated by a '\0' character. Remember strings are nothing but array of characters in c. So here "hi all\0" is stored in contiguous memory locations. So hi all is not an address in itself but test has the pointer to the beginning of the string and knows that it extends till the '\0' character.

    Also in the second case the way you are doing the assignment is illegal.

    You are trying to have an array of integers so you need to do the declaration like this

    int test[] = {1,2,3};

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by roaan View Post
    So hi all is not an address in itself
    If that is the case, then why is

    char *pt = "hi all"

    logical? The address of the pointer is set the be the string.




    Quote Originally Posted by roaan View Post
    Also in the second case the way you are doing the assignment is illegal.
    Why is this? Is it decided that the assignment is legal, or is there a logical explanation of why it is illegal?

    Thank you.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by Niels_M View Post
    If that is the case, then why is

    char *pt = "hi all"

    logical? The address of the pointer is set the be the string.






    Why is this? Is it decided that the assignment is legal, or is there a logical explanation of why it is illegal?

    Thank you.
    I didn't get your first question. What are you trying to ask ?

    Also for the second assignment the way arrays are declared in C that is the correct syntax for them is

    [insert]
    Code:
    int test[] = {1,2,3};
    or for your case if you are trying to get a pointer to the array of integers then you should do like this

    [insert]
    Code:
    int *p;
    int test[] = {1,2,3};
    p = test; // now p has the address to the first element of test

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    My first question is the following:

    Please take a look at the following code

    Code:
    char test[] = "hi all";
    char *pt = test;
    This bit of code makes perfect sence, since test == &test[0].

    Now look at the following code

    Code:
    char *test = "hi all";
    Now this piece of code I do not understand. When declaring a pointer, we should say where it points to, i.e. we have to specify the address. But it is not equalled to an address, but to a string. Why is it we are allowed to do that?

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Okay so thats good that you know that we need to specify the address when assigning something to a pointer as in

    char *pt = test;

    But as i mentioned before that strings are special in C. They are treated as an array of characters. So when i say

    char *test = "hi all";

    what this implies is that the address of the first array element(just consider this for the sake of understanding) which is 'h' in this case is assigned to test. So this is how strings are treated in c.

    If you were to do the following on the test

    printf("\n %c", test); // this would give you 'h'
    so now you see that test is having the address of the first element of the array "hi test". Hope this makes it a bit clearer. So in this case test which is a pointer to a char is pointing to the first character 'h' and you can iterate through the array till you hit the terminating character '\0'.

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	
    	char *str = "Hello Sir";
    	int *p;
    	
    	int test[] = {1,2,3};
    	p = test;
    
    	printf("\n %c", *str);
    	while(*str != '\0')
    	{
    		printf("\n %c", *str);
    		++str;
    	}
    
    	getch();
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    I understand everything in your post. My question is a lot more fundemental: Did K&R decide that when we have

    pointer = character array,

    then pointer gets the address of the first element of the array? I.e., is it just a "coincidence" or is there a logic behind the notation, just like the following piece of code is logical

    Code:
    char test[] = "hi all";
    char *pt = test;
    ?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A string literal, such as "hi all", is stored somewhere in your executable, and thus loaded into memory when the program runs.
    So when you do...
    char* pt = "hi all";
    ...the compiler simply takes the address where the string is stored and saves it in pt.
    You should note, however, that string literals are usually read-only, so any attempt to modify them will almost always fail. Therefore, it is good practice to declare them as const char*, like:
    const char* pt = "hi all";
    This will prevent mistakes trying to modify it.

    As for the question why...
    int* test = { 1, 2, 3 };
    ...isn't possible, that's because that is an initializer list, which is used to fill arrays, not pointers. Another subtle way C works. Also, as roaan explains, when you try to use an array, you really get a pointer to its first element, so we can do this:
    int* ptest = test;
    We have a pointer to the first element. However, we cannot initialize a pointer with the initializer list.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by Elysia View Post
    A string literal, such as "hi all", is stored somewhere in your executable, and thus loaded into memory when the program runs.
    I see. So when I use the assignment "char test[] = "hello" ", I create a string "hello" in the memory, which is not only read-only, i.e. it is not a string literal?


    Quote Originally Posted by Elysia View Post
    As for the question why...
    Code:
    int* test = { 1, 2, 3 }
    ...isn't possible, that's because that is an initializer list, which is used to fill arrays, not pointers.
    I see. I am not quite sure what an initializer list is, but I assume it is not the same as a string, since we are not able to initialize a pointer with it.


    Thanks to both of you so far.
    Last edited by Niels_M; 08-27-2009 at 04:41 AM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For the first question, yes, the string is copied into the array, so it's modifiable.
    For the second question, an initializer list is basically a syntax that allows you to put data into an array (or struct) when you first define it.

    Example:
    int blah[] = { 1, 2, 3 };

    Or
    int blah[3];
    blah[0] = 1;
    blah[1] = 2;
    blah[2] = 3;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by Elysia View Post
    For the second question, an initializer list is basically a syntax that allows you to put data into an array (or struct) when you first define it.
    In that case, a string is also an initalizer list. And since that is the case, how can we initialize a pointer with it?



    Quote Originally Posted by Elysia View Post
    For the first question, yes, the string is copied into the array, so it's modifiable.
    Ok, so when I have char test[] = "hello", "hello" is NOT a string literal?

    Thank you for being patient with me.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Niels_M
    Ok, so when I have char test[] = "hello", "hello" is NOT a string literal?
    "hello" is a string literal used to initialise the char array named test. The contents of this char array can be modified.
    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

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Niels_M View Post
    In that case, a string is also an initalizer list. And since that is the case, how can we initialize a pointer with it?
    A string is not an initializer list.
    And initializer list begins with { and ends with }.
    It is possible to initialize a pointer with an initializer list, like so:

    int* p = { NULL };

    It is also possible to initialize other types of variables.
    But initializer lists are more useful when initializing arrays or structs when you have several members or elements to initialize at the same time. You might think of it as a shortcut for the code above (with the assigning of all the elements in the array).

    The string is a simple assignment, just as:
    char* p = NULL;
    Or even
    int hello = 5;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    I think I get it now. So there is actually a difference between the two following assignments, even though my book says they are equivalent:

    Code:
    char str[] = "test"
    and

    Code:
    char str1[] = {'t', 'e', 's', 't', '\0'}
    The first is a string literal, but the content is also copied into the array str[], and thusly is modifiable. The second is an initializer, and thus I cannot replace str1[] with *str.

    Thanks to all for helping.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Niels_M View Post
    ...and thus I cannot replace str1[] with *str...
    I have no idea what that means.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM