简体   繁体   中英

char pointer pointing to a char array

#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

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.

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

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM