tags:

views:

257

answers:

5
#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?

+13  A: 

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
shodanex
+1 correct answer
Daniel
Yes, good answer. To elaborate, @Chan, it is always good to make types identical so if you have first a char called temp, you pass it as and you have a char again. It's always good to "follow" a variable type when you review your code.
Daniel Daranas
sizeof(arr) is dependent on architecture, but usually 4 (size of pointer). not really sure how CHAN got output of '1', do some compilers overload sizeof() to something similar to strlen() for char*?
Daniel
This code is wrong: main() must return int, sizeof returns size_t, not int, pointers should be printed with %p (and it's only guaranteed to work with void*). Also clrscr() and getch() make the code non-portable for no good reason, and print()'s parameter should be a const pointer.
Bastien Léonard
You are right ! it should be for, unless sizeof knows the size of the array because it is const data ?
shodanex
@Bastien: The code is not printing any pointer.
Daniel Daranas
@Bastien: Also, I'm sure main must return int in C++, but I'm not so sure that's true in C, which is the language of the question. This link says this is not so: http://homepages.tesco.net/J.deBoynePollard/FGA/legality-of-void-main.html
Daniel Daranas
I don't know sizeof spec, but my test case show the behaviour I am expecting.
shodanex
Bastien Léonard
@Bastien: the link basically states that the C standard does not explicitly require that. But I totally agree with your clarification for all practical purposes.
Daniel Daranas
A: 

See here for an explanation of string handling in C: http://www.macdonald.egate.net/CompSci/hstrings.html

Michael Barth
+1  A: 

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]);
Eric Petroelje
+2  A: 

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.

samoz
Your explanation of the result '1' is not correct. Inside the function, 'arr' is a character pointer (not an array) and would return the same as any other pointer (which is normally 4 or 8, not 1).
Jonathan Leffler
I was wondering about why it didn't return 4 actually. The only reason I put this explanation up was because the originally poster said those were his results...
samoz
+2  A: 

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 printfs.

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.)

John Kugelman
The 1 has me puzzled too. I believe that on Crays, at one time, CHAR_BITS was 32, so sizeof(char) == sizeof(short) == sizeof(int) == 1. But that's unlikely to be the platform here; <conio.h> and void main() shouts Windows.
Jonathan Leffler
This sizeof giving 1 is puzzling. If I put temp as global data, I still get 4 in the output of print(). I have seen strange sizeof behaviour, but it was on strange platforms :)
shodanex