简体   繁体   中英

Please explain this strange output

#include<stdio.h>
#include<conio.h>

void print(char *arr);

void main()
{
    clrscr();

    char temp='r';
    print(&temp);
    getch();
}

void print(char *arr)
{
    int arrsz=sizeof(arr);
    printf("size is %d",sizeof(arr));
    printf("char is %c",arr);
}

Why do I get this output?

size is 1
char is e

Surely it should say char is r ?

You print the address not the value use

printf("char is %c",*arr);

Try to run this through a debugger to understand what happens, and please ask a real question, like what do you think should happen, and what you observe instead. By doing this you will probably answer yourself to most of your question.

By the way, once in print, arr is a local variable, and sizeof as no way to know the size of the original array, so it should print size is 4. The code below shows this behavior, and a difference between array and pointers when it comes to sizeof. If you try

EDIT: changed code to something I actually tested, rather than just guessed, thanks to Daniel's comment

#include <stdio.h>

void print(char *);

int main(int argc, char ** argv)
{
    char temp = 'r';
    char temp2[] = {'a','b'};
    char temp3[] = {'r'};

    print(&temp);
    print(temp2);
    print(temp3);

    printf("sizeof temp is %d\n",sizeof(&temp));
    printf("sizeof temp2 is %d\n", sizeof(temp2));
    printf("sizeof temp3 is %d\n",sizeof(temp3));
    return 0;
}

void print(char * arr)
{
    printf("size of arr is %d\n", sizeof(arr));
    printf("arr contains %c\n", *arr);   
}

You get :

size of arr is 4
arr contains r
size of arr is 4
arr contains a
size of arr is 4
arr contains r
sizeof temp is 4
sizeof temp2 is 2
sizeof temp3 is 1

You got size equal to 1 because the sizeof(arr) is returning the size of what arr points to, a character, which is size 1.

You got 'e' because printf was given a char* instead of a char when the %c flag was given. Change it to printf("Char is: %c", *arr).

If you want to use a char*, you need to use the %s flag in printf. But also, make sure to null terminate whatever you pass printf or you will get garbage.

Hm, something is wrong here. I don't believe this output.

  • arr is a char * and sizeof(char *) != 1 .
  • There are no '\\n' s in your printf s.

If you typed in your code by hand, please don't do that. Copy and pasting is far better so we can be sure you didn't make any typos. As written you should've gotten something like

size is 4char is 0

(The "char is" part could print any random character.)

You're trying to use this as both a string and a character (and your string is not null terminated). To use it as a string, you would change the code like this:

//char temp='r';
char* temp = "r";

...

//printf("char is %c",arr);
printf("char is %s",arr);

To use it as a char, you would change your print statement like this:

printf("char is %c", arr[0]);

有关C语言中字符串处理的说明,请参见此处: http//www.macdonald.egate.net/CompSci/hstrings.html

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