0

I don't know what I'm doing wrong I'm stuck on this for the past two hours. Any help would be highly appreciated.

My code is the following:-

#include <stdio.h>
int* findLargest(int* arr,int size){
    return &arr[size-1];
}

int main(){

    int size=3;
    int arr[3]={3,4,5};

    int* largest=&arr[size-1];
    int* largest2=malloc(100);
    largest2=findLargest(*arr,size);
    printf("%d",largest);
    printf("%d",*largest2);
}

I get a garbage value when I execute.

9
  • 2
    Read the warnings and errors when you compile. Commented Nov 5, 2017 at 12:07
  • What is the expected output? Commented Nov 5, 2017 at 12:08
  • Never mind I figured it out. Commented Nov 5, 2017 at 12:09
  • I just didn't have a \n between the two printfs and the first was returning a garbage value which it should because it is a pointer Commented Nov 5, 2017 at 12:09
  • 2
    Always check compiler warnings. findlargest.c:16:26: warning: passing argument 1 of ‘findLargest’ makes pointer from integer without a cast Commented Nov 5, 2017 at 12:10

4 Answers 4

3

Only pass arr to findLargest, this is already a pointer

largest2=findLargest(arr,size);

printf contents of largest, not the pointer itself

printf("%d\n", *largest);

Also, I'm not sure why you have the line:

int* largest2=malloc(100);

The malloc'd pointer is immediately lost in the next line when you assign to largest2 again.

I would also consider assigning size like so:

int size = sizeof(arr)/sizeof(arr[0]);

This is safer if you change the contents of arr in the future.

With all these changes main looks like:

int main()
{
    int arr[3]={3,4,5};
    size_t size = sizeof(arr)/sizeof(arr[0]);

    int* largest=&arr[size-1];
    int* largest2=findLargest(arr,size); // Only pass array to findLargest, this is already a pointer
    printf("%d\n", *largest); // printf contents of largest, not the pointer
    printf("%d\n", *largest2);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nitpicking: The sizeof-operator evaluates to a size_t, not to an int.
1

The problem here is this line

printf("%d",largest);

Where you're not de-referencing the pointer. Nor is there a space or new line after the value causing more confusion.

Comments

0

you can't edit address of pointer, you've to edit value of pointer
like this:

int * v = malloc(sizeof(int)); //WRONG
int n=54;
v=&n;

int * v = malloc(sizeof(int)); //CORRECT
int n=54;
*v=n;

Comments

-2

Well, the problem was simple. Just in case someone runs into this confusion this can help.

The correct code is:

#include <stdio.h>
int* findLargest(int* arr,int size){
    return &arr[size-1];
}

int main(){

    int size=3;
    int arr[3]={3,4,5};

    int* largest=&arr[size-1];
    int* largest2=malloc(100);
    largest2=findLargest(arr,size);//Not findLargest(*arr,size)
    printf("%d",*largest2);
}

2 Comments

This occurs memory leak.
Well in your new code then, the "largest" variable is useless. Also what @BLUEPIXY said. Use free(largest2) to release the pointer.

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.