1
int* dec2bin(int y){
    int *arr = (int*)malloc(sizeof(int)*5);
    int i;
    
    for (i=0; i<5; i++) arr[i]=0;
    
    return arr;
}

In this code, I write 0 to arr[0] through to arr[4], but the function returns 1070192. I want to return 00000.

What am I doing wrong?

Neuron
  • 5,141
  • 5
  • 38
  • 59
redchicken
  • 311
  • 1
  • 5
  • 18
  • 4
    Start by not casting the return value of malloc. – m0skit0 May 05 '15 at 14:45
  • 6
    Again, [do _not_ cast the return value of `malloc`](http://stackoverflow.com/questions/20094394/why-do-we-cast-return-value-of-malloc) – starrify May 05 '15 at 14:46
  • Welcome to Stack Overflow! Warning: when calling `sizeof` in `malloc` (and the like) [you should always write it](http://stackoverflow.com/a/17258659/1151654) as `ptr = malloc(sizeof(*ptr) * ...);` instead of `ptr = malloc(sizeof(ptrtype*) * ...);`. – Eregrith May 05 '15 at 14:46
  • 1
    disregarding the fact, that we can't see, where you are calling the returned part, so we can't figure out WHY. thats what you expect as beeing returned. We also can't even guess a notation without that information, so that "But this function return the value 1070192." is just a bubble without any content. as I would bet there isn't anything returned expect `0`'s and `1`'s everything else is intepretation of any layer, which you don't even share with us ;) – dhein May 05 '15 at 14:48
  • @starrify Silly, but http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc is a better link since the one you provide is to a dupe-closed question. – unwind May 05 '15 at 14:50
  • 2
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. – Lundin May 05 '15 at 14:56

3 Answers3

5

Most probably you are printing the address returned by malloc().

You need a loop to print the contents, for example

int *dec2bin(int size)
{
    return calloc(size, sizeof(int));
}

int main(void)
{
    int *data;
    int  index; 
    int  size;

    size = 5;
    data = dec2bin(size);
    if (data == NULL)
        return -1; /* allocation error */
    for (index = 0 ; index < size ; index++)
        printf("%d", data[index]);
    printf("\n");
    free(data);

    return 0;
}

You can see that I used calloc(), that's because you are going to initialize all the values to 0, otherwise use malloc() instead.

haccks
  • 104,019
  • 25
  • 176
  • 264
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • @haccks It's not a blind guess; it's the answer to the problem. If iharob hadn't written it, I would. – Carey Gregory May 05 '15 at 14:52
  • 2
    @Zaibis that is not a really good reason for the -1, There is no other explanation. Also, note that the OP says, *This function returns the value ...* – Iharob Al Asimi May 05 '15 at 14:53
  • 1
    @CareyGregory; I never said its not an answer. – haccks May 05 '15 at 14:54
  • 2
    @Zaibis In that case the question should be put on hold as "unclear what you are asking"/"cannot be answered in current form" etc. Don't down vote answers just because the question is unclear, but judge answers based on their own merit. If the question really can't get answered in its current form, then all answers will go away with it too when it gets closed/deleted. – Lundin May 05 '15 at 14:55
  • @haccks Good idea, I should not say that like I am sure of it, it's the most probable reason. – Iharob Al Asimi May 05 '15 at 14:56
  • @Lundin: thats exactly what I did and what is my view. But a guessed answer won't help for alter cases. so it isn't worth a answer (thats why I didn't provided one of my own) – dhein May 05 '15 at 14:59
  • @Zaibis; Someone is downvoted your answer doesn't mean you have to downvote his answer too. I think honesty is still on SO. This answer deserves no downvote. Any experienced user will give the same reason as given in this answer. – haccks May 05 '15 at 15:00
  • @Zaibis I suppose now I know why although my answer was accepted it has a downvote. – Iharob Al Asimi May 05 '15 at 15:06
  • @Zaibis; You are an old member. Stop fighting. I can see there is a downvote on iharob answer too and I can guess why. Both answers there answers the question. – haccks May 05 '15 at 15:07
  • @Zaibis that was actually a good question, it was answered before, but it was a good question. – Iharob Al Asimi May 05 '15 at 16:23
  • 1
    @haccks: , iharob: I did as I said. As now your answers have no connection anymore, you should remove that, too. I un-downvoted because of haccks edit AND because the OP is still not closed, so probabbly ON-Topic. – dhein May 06 '15 at 13:25
1

The function returns pointer to the allocated memory

int* dec2bin(int y){
    int *arr = (int*)malloc(sizeof(int)*5);
    int i;

    for (i=0; i<5; i++) arr[i]=0;

    return arr;
}

for an array of 5 integers. Its address may not be equal to 0 unless the allocation failed. But the integers themselves are equal to 0.

If you will write the call for example the following way (it is not clear the meaning of the parameter)

int *p = dec2bin( 0 );

then *p, the first element of the array, will be indeed equal to 0

Or even you can write

int *p = dec2bin( 0 );

for ( int i = 0; i < 5; i++ ) printf( "%i ", p[i] );

and you will get that all elements of the array are equal to 0.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

If you would like to get value of that array as a string (array of characters), below is the code you can start off by.

char* dec2bin(int y) {
    int *arr = (int *) malloc(sizeof(int) * 5);
    char output[50];
    int i;

    for (i=0; i<5; i++) arr[i] = 0;
    sprintf(output, "%d%d%d%d%d", arr[0], arr[1], arr[2], arr[3], arr[4]);

    return output;
}

For printing it out, you could use:

char *string = dec2bin(0);
printf("%s", string);
frogatto
  • 28,539
  • 11
  • 83
  • 129