Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im new to programming, there is some part that made me confuse when learning pointer.


C++
int main()
{
    char string[]="hello";

    char *my_pointer=string;

    printf("first character is %c", *my_pointer);
    return 0;
}

output : first character is h

code 2:

C++
int main()
{    
    char array_of_words[]="one\0two\0three";

    char *my_pointer=array_of_words;

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

    return 0;
}

output : one

What I have tried:

questions:

I'm confuse here, the printf function part in the first code using asterisk symbol means to point what is inside pointer (my_pointer) which is the address of variable string that refer to the pointer string for an array that point the memory address of the first word in array "hello". is my understanding true?

and also when I change %c in printf("first character is %c", *my_pointer); to %s, the program crash. i want to know why i cant use %s and what is the different if i dont use asterisk in my_pointer in printf("first character is %c", my_pointer);

While in second code *my_pointer=array_of_words copy the address of the first content of array_of_the_words which is 'o'? And char *my_pointer=array_of_the_words means we want *my_pointer has same address as array_of_the_words which refer to'o'(?) if my_pointer in printf("%s\n",my_pointer) refers to memory address of *my_pointer Then why not it produce 'one' and not produce 'o' ??

Actually it copy the first string address but contain all the words?
But why the first code use *my_pointer in printf and second code my_pointer in printf?
I know that *my_pointer refer to value and my_pointer refer to address but why the output is different
Posted
Updated 8-May-17 21:57pm
v2

Another way to look at the asterisk is as the pointer dereference operator. It obtains the value of where pointer is aimed which for an array is the first item of the array.

In printf, %s refers to a string, an array of characters so the address of the array needs to be passed. The %c format flag is for a single character so one member of the array is expected by printf for that specifier. The output is different because the format specifiers tell the printf function how to interpret the data it is passed. Data is passed on the stack to the function in the form of either a pointer or a value. If the format is wrong for the data on the stack then the function will either give strange output or it will crash the program.
 
Share this answer
 
Comments
Member 13188016 9-May-17 1:26am    
Thankyou for answering my questions!!!
But still confuse in some part, So the value of where pointer aimed in array always the first item of an array, But actually inside the poiner is it contains all the words address or only the first item address?
It means that *my_pointer only contains the address of first word?
While my_pointer refer to the address of whole words?
Is there any relation with data type char too?

char *my_pointer;
my_pointer = string;
Since my_pointer is a pointer to char, the value of my_pointer is an address which points to char.
Data type char only refer to one word like 'h' for example or all words?
Rick York 9-May-17 19:59pm    
The pointer aims at the first item of the array but that location in memory is just the first item. The other items proceed it in memory. This is how arrays work.

The phrase *my_pointer means the value at the address contained in my_pointer.

This convention applies to all data types, not just characters.
Because they are really different beasts. I would suggest you reading a tutorial on the topic, like, for instance: Everything you need to know about pointers in C[^].
 
Share this answer
 
Comments
Member 13188016 9-May-17 22:47pm    
Thankyou for the recommendation!!! Will read it for better understanding!!
CPallini 10-May-17 4:10am    
You are welcome.
Case 1:
C++
printf("first character is %c", *my_pointer);

You are telling printf to output a single character (%c) so the parameter after the format string must be a single character. Since the character is inside an array pointed to by my_pointer it needs to be extracted from the array, and the way to do that is to use the dereferencing operator (*), which will return the single character from the location pointed to.

Case 2:
C++
printf("%s \n",my_pointer);

In this case you are telling printf to output a sequence of characters (%s) so you must pass the address of that sequence. And since my_pointer points to the start of the array of characters, that is what it will do. Internally printf will do something like:
C++
int printf(char* format, char* string)
{
    // process the format and find %s ...
    while (*string != '\0') // repeat until end of string
    {
        char c = *string;  // get the next character from the pointer address
        // print this character to the terminal
        ++string; // advance the pointer to the next character
    }
}
 
Share this answer
 
Comments
Member 13188016 9-May-17 22:46pm    
Thankyou!!!!!!! So the pointer data type determine its value?
For example char *my_pointer so the value always only one word like the first word of an array(?)
If int *my_pointer then the value is number?
For an array *my_pointer obtain value where pointer is aimed which is the first term of an array so that's why the data type is always char for an array?
Richard MacCutchan 10-May-17 3:25am    
Well, not quite. A pointer's attributes are determined by the data type that it points to. So a char* points to a single character, and can be incremented one character at a time to access each element of a string (array of char). An int* points to a 32 bit integer type and can increment through an array of integers one at a time. Other pointers can access long, double or even structure types, etc. Take a look at Introduction to Pointer in C Language | C Language Tutorial | Studytonight[^] for more information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900