I have the following code:
#include <stdio.h>
#include <stdlib.h>
// helping
void sortint(int numbers[], int array_size)
{
int i, j, temp;
for (i = (array_size - 1); i > 0; i--)
{
for (j = 1; j <= i; j++)
{
if (numbers[j-1] > numbers[j])
{
temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
}
}
}
}
// exer1 - A
void sort(int** arr, int arrsize) {
int i = 0;
// sort....
for(; i < arrsize; ++i) {
sortint((*(arr+i))+1, **(arr+i));
}
}
// Exer1 - B
void print(int** arr, int arrsize) {
int i = 0, j, size, *xArr;
for(; i < arrsize; ++i) {
size = **(arr+i);
xArr = *(arr+i);
printf("size: %d: ", size);
// print elements
for(j = 1; j <= size; ++j) printf("[%d], ", *(xArr+j));
printf("\n");
}
}
// Exer2:
void exera() {
int* ptr = (int*)malloc(sizeof(int));
if(!ptr) exit(-1);
eb(ptr);
free(ptr);
}
void eb(int* ptr) {
int* arr = (int*) malloc(sizeof(int) * (*ptr));
int i = 0;
for(; i < *ptr; ++i) scanf("%d", arr+i);
ec(arr, *ptr);
}
void ec(int* arr, int size) {
int i;
sortint(arr, size);
for(i = 0; i < size; ++i) printf("[%d], ", *(arr+i));
}
int main() {
// Exer1:
int a[] = {4,3,9,6,7};
int b[] = {3,2,5,5};
int c[] = {1,0};
int d[] = {2,1,6};
int e[] = {5,4,5,6,2,1};
int* list[5] = {a,b,c,d,e};
sort(list, 5); // A
print(list, 5); // B
printf("\n\n\n\n\n");
// Exer2:
exera();
fflush(stdin);
getchar();
return 0;
}
I get these errors:
Error 2 error C2371: 'eb' : redefinition; different basic types
source.c 56
Error 4 error C2371: 'ec' : redefinition; different basic types
source.c 63
Warning 1 warning C4013: 'eb' undefined; assuming extern returning int
source.c 52
Warning 3 warning C4013: 'ec' undefined; assuming extern returning int
source.c 60
I tried to change function names - for nothing.
Why is that error is being shown? I'm using Visual C++ Express 2010.
You are trying to call eb
and ec
before they are declared or defined. Move the definition of ec
before eb
and both before exera
. You could also forward declare your functions before you define any of them like so:
void eb(int* ptr) ;
void ec(int* arr, int size) ;
You call eb
from exera
, before eb
is declared. The compiler assumes it'll return int
then finds an implementation that returns void
further down the file.
The most common fix is to declare your local functions at near top of your file
void eb(int* ptr);
// repeat for each other function which generates the same error
If you are going to place the function eb
after the point at which it is called, then you need to place a prototype for it before it is called... otherwise, C will use the default prototype and then your function ends up redefining it, thus the error you received.
Alternatively, you can move the functions themselves before they are used in the source file, but that's not always possible. Placing prototypes at the top of the file or, better still, in a header file you can include anywhere you will use the functions is the best alternative.
If you don't find multiple definitions, the most probable reason could be the headers are getting included multiple times.
In this case use header protection macro.
#ifndef HEADER_FILE
#define HEADER_FILE
/*Content of your files*/
#endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With