Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a beginner. As far as I know, to use a pointer first you have to give the new variable a address of the memory and then you can call the variable with * this symbol and modify it.

<pre lang="C++">
<pre>

#include <stdio.h>

int main()
{

    char str[20] = "GeeksforGeeks";

    // Pointer variable which stores
    // the starting address of
    // the character array str
    char* ptr = str;

    // While loop will run till 
    // the character value is not
    // equal to null character
    while (*ptr != '\0') {
        printf("%c", *ptr);

        // moving pointer to the next character.
        ptr++; //dont do *ptr 
    }

    return 0;
}


What I have tried:

This is just a random question which I could not find any answer from google
Posted
Updated 24-Oct-22 18:05pm

Quote:
Why this programming is acting like a pointer without taking the address of the variable?

C++
char* ptr = str;
//          ^^^ Because it takes the address of the variable here !
 
Share this answer
 
The C Language specification says that the name of an array is a pointer to the first element of that array.
So this line:
C
char* ptr = str;
Copies a address from one pointer to another. It also means that these two are equivalent:
C
char c = *ptr;

C
char c = ptr[0];
Because a pointer can be treated as an array!

Additionally, because C doesn't have any concept of boolean values true or false, it treats any non-zero value as true and any zero value as false. Which means that this line:
C
while (*ptr != '\0') {
can be simplified to this:
C
while (*ptr) {


And do yourself a couple of favours: pick an indentation style and stick to it - be consistent! You have mixed two styles in that code:
C
int main()
{
    ...
    while (*ptr != '\0') {
        ...
    }
    return 0;
}
Which while it is readable in a tiny fragment of code like this rapidly becomes unwieldy as your code gets bigger. Get into good habits early, and you won't have so many problems later!

Then stop commenting what code does: comment why it does it! All of your comments say what a line of code does:
C
// Pointer variable which stores
// the starting address of
// the character array str
char* ptr = str;

C
// While loop will run till
// the character value is not
// equal to null character
while (*ptr != '\0') {

C
// moving pointer to the next character.
ptr++;
What do they add to the application? How do they help anyone understand what it does? Use comments that help, not that describe code - because if nothing else, they will get "left behind" when you change the code and will contradict what the app is actually doing! So if you trust the comments, you'll believe the code does this and wonder why it actually did that!

And use sensible names for variables: str is just about OK, but ptr tells me nothing: what is it a pointer to? What is it used for? Pick a name that reflects usage, not one that is easy to type: tempStr perhaps - it's temporarily being used to navigate through str after all!
Again, this is less important in a tiny fragment of code, but as it gets bigger and more complex, it becomes much more important!
 
Share this answer
 

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