-1

I'm trying to make a sort function to sort the order of float value incompatible pointer. and when u run the code, after typing the size of pointed array and input the value, then the running just stopped. I do not know where the problem is, any one can help. I already corrected the warning, but still now result for running the code

#include <stdio.h>
#include <stdlib.h>

void sort(const int size, float *input, float *output);


int main(void) {
    int a;

    float *b=&b1;

    float *c=&c1;
    int i, i1;

    printf("input the size\n");
    scanf("%d", &a);
     b=(float*)malloc(sizeof(int)*a);
     c=(float*)malloc(sizeof(int)*a);
    for(i=0; i<a ; i++){
        scanf("%f", &b[i]);

    }
    for(i1=0; i1<a; i1++){
        c[i1]=b[i1];
        printf("%f\n", c[i1]);
    }

    sort(10, b, c);
    free(b);
    free(c);
    return 0;
}

void sort(const int size, float *input, float *output)
{
    void swap( float *element1Ptr,  float *element2Ptr);
    int pass;
    int j;
    int i0;

    for (pass=0; pass<size-1;pass++)
    {
        for (j=0; j<size-1;j++){
            if(input[j]>input[j+1]){

            swap(&input[j], &input[j+1]);
            }

        }
    }


    for (i0=0; i0<size; i0++){
        output[i0]=input[i0];
        printf("%f", output[i0]);
    }

}

void swap( float *element1Ptr,  float *element2Ptr)
{
float hold=*element1Ptr;
*element1Ptr=*element2Ptr;
*element2Ptr=hold;
}
user3595689
  • 33
  • 1
  • 6

3 Answers3

1

Fix 1: First you are assigning two float addresses to pointers c and d-

float *b=&b1;

float *c=&c1;

Then you are allocating memory for it. It has no meaning at all. when you allocate memory the newly allocated memory address is returned to the pointer b and c.

if you want to make 0 to all allocated memory you can use calloc to allocate memory. because it will allocate the memory and clear the data in it and give it to user

float *b= (float *)calloc(a,sizeof(float));

float *c= (float *)calloc(a,sizeof(float));

Fix 2: You are having float pointer. but after allocating memory you are typecasting the memory as int *-

float *b=&b1;

but

 b=(int*)malloc(sizeof(int)*a); // don't do this

Instead use-

 b=malloc(sizeof(float)*a);

Fix 3: With out allocating memory for float *c you are assigning values to it-

for(i1=0; i1<a; i1++){
    c[i1]=b[i1]; // note here. you have not allocated memory for c before
    printf("%f\n", c[i1]);
}

Allocate the memory for float *c and do it.

c = malloc(sizeof(float)*a);

A simple program to do your work-

#include <stdio.h>
#include <stdlib.h>
void sort(const int size, float *input);

int main(void) {
        int a,i;
        float *b;

        printf("input the size\n");
        scanf("%d", &a);
        b=(float*)malloc(sizeof(float)*a);
        for(i=0; i<a ; i++){
                scanf("%f", &b[i]);
        }

        sort(a, b);

        for (i=0; i<a; i++)
                printf("%f\n",b[i]);

        free(b);
        return 0;
}

void sort(const int size, float *input)
{
        int pass,j,temp;
        for (pass=0; pass<size-1;pass++)
        {
                for (j=0; j<size-1;j++){
                        if(input[j]>input[j+1]){
                                temp = input[j];
                                input[j]=input[j+1];
                                input[j+1]=temp;
                        }
                }
        }
}

Don't use unnecessary variables, Other then the important ones! If you want a copy of your input, copy it to another array and to the sorting on the output array, not on input array!

Sathish
  • 3,740
  • 1
  • 17
  • 28
1

There were a couple of bugs in your code.

No memory was allocated for c.

You modified the input array in sort.

One print loop looped to 10.

Also, I cleaned up the formatting a bit.

I move the forward declaration so that it is outside the sort function. Not a bug, but programmers expect forward declarations to be put outside any function.

I removed unecessary printf statements and print only the sorted array.

#include <stdio.h>
#include <stdlib.h>

void sort(const int size, const float *input, float *output);
void swap( float *element1Ptr,  float *element2Ptr);


int main(void) {
    int a;
    float *b;
    float *c;
    int i, i1;

    printf("input the size\n");
    scanf("%d", &a);
    b = malloc(sizeof(float)*a);
    c = malloc(sizeof(float)*a);

    for(i=0; i<a ; i++){
        scanf("%f", &b[i]);
    }
    sort(a, b, c);

    for(i1=0; i1<a; i1++){
        printf("%f\n", c[i1]);
    }

    free(b);
    free(c);
    return 0;
}

void sort(const int size,  float const *input, float *output)
{
    int pass;
    int j;
    int i0;

    for (i0=0; i0<size; i0++){
        output[i0]=input[i0];
    }

    for (pass=0; pass<size-1;pass++)
    {
        for (j=0; j<size-1;j++){
            if(output[j]>output[j+1]){
                swap(&output[j], &output[j+1]);
            }
        }
    }
}

void swap( float *element1Ptr,  float *element2Ptr)
{
    float hold=*element1Ptr;
    *element1Ptr=*element2Ptr;
    *element2Ptr=hold;
}

General advice:

Turn up the warning level of the compiler. Compiler warnings are there for a reason.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
0

You defined variable b as having type float *

float *b=&b1;

but are trying to assign pointer to int to that variable

 b=(int*)malloc(sizeof(int)*a);

First of all there is no sense in the initialization of b

float *b=&b1;

and secondly it seems you want to allocate an array of floats. So you have to write

 b = ( float * )malloc( sizeof( float ) * a );

Also you did not allocate memory pointed to by c. So this code

for(i1=0; i1<a; i1++){
    c[i1]=b[i1];
    printf("%f\n", c[i1]);
}

sort(10, b, c);

is invalid and the program has undefined behaviour. It is also unclear why you are using magic number 10 in sort instead of variable a.

And what is the sense in defining variables b1 and c1?

float b1=0;
float c1=0;

It seems they are not used.

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