1

Here is a program, I write it to output all the characters of a string one by one. But I also print the address of individual blocks of the array. The problem is addresses for all blocks are same. Why? Does someone know?

#include<stdio.h>
int main()
{
    char enter[]="Kinsman";
    char *ptr;  
    ptr=enter;
    int i=0;
    while(*ptr!='\0')
    {
        printf("%c%p\n",*ptr,&ptr);
        ptr++;
        for(i=0;i<=100000000;i++);
    }
return 0;
}
8
  • 5
    for(i=0;i<=100000000;i++); ?? why this Commented Oct 30, 2013 at 10:50
  • 1
    @GrijeshChauhan Back on my old ATARI 800XL, waiting one second was accomplished easily with FOR I=1 TO 500: NEXT I. But this method - waiting in terms of cycles - has been outdated for about 20 years. Commented Oct 30, 2013 at 11:17
  • @glglgl Yes I read it somewhere in past and it will consume CPU cycles. ~~ But a C compiler can optimize it by replacing i = 100000000;.. it is not reliable. Correct? Commented Oct 30, 2013 at 11:22
  • @GrijeshChauhan I used it just to give a pause for each output through loop. Commented Oct 30, 2013 at 11:28
  • 1
    @AshishTomer It is better to use the appropriate function - sleep() or usleep() on Unix, Sleep() on WIndows. Commented Oct 30, 2013 at 11:36

3 Answers 3

4

Because you print the address of the actual pointer.

When you use &ptr you get the address of the actual pointer and not the address if points to. Remove the ampersand (address-of operator &) so you have only ptr.

Sign up to request clarification or add additional context in comments.

7 Comments

Yes you are right. Thank you! :) Can you tell me how do I clear screen on startup using clrscr()?
@AshishTomer By calling the function?
No i know that actually I added conio.h but GCC gives error: "reverse.c:2:18: fatal error: conio.h: No such file or directory #include<conio.h> ^ compilation terminated."
@AshishTomer What is the point of clearing the "screen"? As a user, this would annoy me as I lose track of what happened before. Is this really absolutely necessary?
@glglgl no! It's not necessary. I just wanted to :P :D
|
3

You are printing the address of the pointer, not the value of the pointer

Try

printf("%c%p\n",*ptr, static_cast<void*>(ptr));

(https://stackoverflow.com/a/18929285/259)

7 Comments

I printed the value and address both using %c and %p
@AshishTomer Yes but you also added &, read answer again. &ptr should be just ptr in printf
@AshishTomer: You might want to see this post for clrscr.
Using %p without a void * is undefined behavior. Unless the argument is a ptr-to-void, a cast is required.
@DavidSykes what are standards? And where can I get them?
|
1

ptr is a pointer and it is also a variable in stack that has an address. This is fixed, while what it points to is varying by ptr++, thus you've to print the pointed-to value and not the address of the pointer itself.

 printf("%c%p\n",*ptr, (void*)ptr);
 //                   ^  remove & , and add void*

1 Comment

Can you tell me how do I clear screen? <br/>I added conio.h but GCC gives error: "reverse.c:2:18: fatal error: conio.h: No such file or directory #include<conio.h> ^ compilation terminated."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.