1
#include<stdio.h>
int main(){
 char a[3];
 char *b=NULL;
 a[0]=0;
 a[1]=1;
 a[2]=2;
 b = a;
 printf("%c",b);
 b++;
 printf("%c",b);
 b++;
 printf("%c",b);
 return 0;
}

I tried to print the values 0,1,2 by incrementing the pointer by 1. please help

1
  • 1
    Sounds like homework. What's your analysis of the problem so far? Commented Jul 20, 2012 at 14:55

3 Answers 3

4

b is a pointer in itself, you have to dereference it to get the actual values:

printf("%d", *b);
b++;
printf("%d", *b);
b++;

etc.

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

Comments

3

%c tells printf to interpret the char argument as a character code (most likely ASCII). Use %d instead.

Comments

2
#include<stdio.h>
int main(){
 char a[3];
 char *b=NULL;
 a[0]='0';
 a[1]='1';
 a[2]='2';
 b = a;
 printf("%c",*b);
 b++;
 printf("%c",*b);
 b++;
 printf("%c",*b);
 return 0;
}

Comments

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.