Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <stdio.h>
int compare(void *p,int a[],int n);
int main()
{
    void *p;
    int n,i,j,c;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    compare(&p,a,n);
    for(i=0;i<n;i++)
    {
        printf("%d",*(int*)p+i);
    }
    
    return 0;
}
int compare(void *p,int a[],int n)
{
    int i,j;
    p = a;
    int f = 0;
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(*(int*)p+i > *(int*)p+j)
            {
                f = 1;
            }
        }
    }
    if(f==1)
    {
        for(i=0;i<n;i++)
        {
            return *(int*)p+i;
        }
    }
}


What I have tried:

I have tried to get some output but it is not returning the correct value.

HELP ME WITH THIS.
Posted
Updated 31-May-20 23:11pm
v2
Comments
Richard MacCutchan 1-Jun-20 4:17am    
Don't use void*, use the proper type, which in your case is int*.
KarstenK 1-Jun-20 4:32am    
your p hasnt a proper value when compare is called? The others are absolute right about the rest.

Where is the point in passing a void pointer into your function if the first thing you are going to do with it is throw it away?
C++
int compare(void *p,int a[],int n)
{
    int i,j;
    p = a;

Seriously, 1 minute with the debugger would have shown you that: so get used to it - it's thE most powerful tool you have!
 
Share this answer
 
Quote:
What's error in this program I used VOID POINTER

I have not used C for a long time, but are you sure about void pointer?

Quote:
I have tried to get some output but it is not returning the correct value.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
The following will not compile, because a the value of 'n' must be know at compile time (not runtime).
scanf("%d",&n);
int a[n];
You must do a runtime memory allocation
    scanf("%d",&n);
    int *a = malloc(sizeof(int) * n);
... // work with array, then free it when finished
    free(a);
    return 0;
Your compare function takes a pointer (compare(void *p, int a[], int n)) as the first parameter and you are passing it the address of a pointer (compare(&p, a, n)), which is a pointer to a pointer (**p). Therefore, I conclude that your compare function should be taking a pointer to a pointer (compare(void **p, int a[], int n)).

This will change how you refer to the 'p' variable in the comparison function:
instead of
*(int*)p
you would use
*(int*)*p
Additionaly, Your compare function either returns an undefined value if 'f' is not equal to '1' or whatever value is stored in a[0].

You need to go back to the drawing board and find out exactly what you are try to accomplish with this code. Because whatever it is, this is not it.

Write a detailed description of the compare function. What is the expected input and output? This is what you need to test at the function call level.

Take each line apart to see what it is doing. Write out a description of the line in English (or language of choice). If you can do that, then you understand it; otherwise you do not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900