Reputation: 1
I want to know why did i get this error? "D:\C Programming\C training projects\bubblePointer\main.c|6|error: expected ';', ',' or ')' before numeric constant". I need to understand why< however the code in "C how to program" pass size of array also
#include <stdio.h>
#include <stdlib.h>
#define size 10
//prototype
void bubble(unsigned int a[],const size_t size);
void swap(unsigned int *ptr1,unsigned int *ptr2);
int main()
{
unsigned int response[size] =
{6, 7, 8, 9, 8, 7, 8, 9, 8, 9};
bubble(response,size);
return 0;
}
void bubble(unsigned int a[],const size_t size)
{
int pass;
for(pass=0;pass<size-1;pass++)
{
int j;
for(j=0;j<size-1;j++)
{
// swap adjacent elements if they’re out of order
if(a[j]>a[j+1])
{
swap(&a[j],&a[j+1]);
}
}
}
// output sorted array
size_t i;
for ( i = 0; i < size; ++i)
{
printf("%4d", a[i]);
}
}
void swap(unsigned int *ptr1,unsigned int *ptr2)
{
unsigned int hold;
hold=*ptr1;
*ptr1=*ptr2;
*ptr2=hold;
}
Upvotes: 0
Views: 172
Reputation: 51825
Your #define size 10
instructs the pre-processor to replace all instances of the token size
with 10
. Thus, your declaration (and definition) of the function:
void bubble(unsigned int a[],const size_t size);
will be converted to:
void bubble(unsigned int a[],const size_t 10);
before the actual compilation process gets to work.
Can you see what's wrong with that second definition? Of course, you can't use 10
as the name of a parameter.
What you need to do is: either rename that parameter (say, size_arg
) or (depending on your reason for defining the size
macro in the first place) remove that parameter from the function and just use that predefinition in the function's code.
Upvotes: 3