"Old-style parameter declarations" error - c

I'm having two compile errors when trying to compile my code, and I can't find what the issue really is. Could anybody help shed some light?
error: old-style parameter declarations in prototyped function definition
error: 'i' undeclared (first use in this function)
Code:
void printRecords (STUREC records[], int count)
STUREC records[ARRAY_MAX];
int count;
int i;
{
printf("+---------------------------+--------+--------+--------+--------+--------+--------+---------+-------+\n");
printf("| Student Name | ID | Test 1 | Test 2 | Proj 1 | Proj 2 | Proj 3 | Average | Grade |\n");
printf("+---------------------------+--------+--------+--------+--------+--------+--------+---------+-------+\n");
for (i = 0; i < count; i++)
{
size_t j;
printf ("|%s|%d|%d|%d|%d|%d|%d|%f|%c|", records[i].name, records[i].id, records[i].score1,
records[i].score2, records[i].score3, records[i].score4, records[i].score5,
records[i].ave, records[i].grade);
}
return;
}

Not for this specific question, but if you happen to come across this error, check that you did not miss a semicolon at the end of your declaration, because that's what happened to me.

If you want to use old style C parameter declarations, you need to do this:
void printRecords(records, count)
STUREC records[ARRAY_MAX];
int count;
{
int i;
// ... rest of the code ...
}
But this is not considered a good practice and can make your code harder to read. Some compilers have even stopped supporting this syntax.
The other comments/answers are saying that you re-declare (and therefore hide) your function parameters in the body of the function, but this is not what you want to do (otherwise you effectively lose the parameters being passed in).
If you define a function like this:
void fxn(int num) {
int num;
num = num;
}
What does num refer to: the parameter or the local variable?
Either do this:
void printRecords(records, count)
STUREC records[ARRAY_MAX];
int count;
{
int i;
// ... rest of the code ...
}
or do this:
void printRecords(STUREC records[], int count)
{
int i;
// ... rest of the code ...
}
But don't try to do both or a mixture of the two.

You have
void printRecords (STUREC records[], int count)
STUREC records[ARRAY_MAX];
int count;
int i;
{
But I guess you want:
void printRecords (STUREC records[], int count)
{
int i;
EDIT:
Thanks to callyalater for noting the redeclaration of the parameters in the function...

Related

How would I get this code to be executed without using stdlib.h

I keep receiving an error regarding malloc and I'm trying to find out how to get this code to work without using stdlib.h in the header. Just stdio.h, is this possible and how? As I'm totally confused
#include <stdio.h>
void allocate(int* score_array, const int input)
{
int iter;
for(iter = 1;iter <= 11;++iter)
{
if( (input < iter*10) && (input >= (iter-1)*10 ) )
{
++(score_array[iter-1]);
}
}
}
void printf_star(const int len)
{
int iter;
for(iter = 0;iter < len;++iter)
{
printf("*");
}
printf("\n");
}
int main()
{
int iter, size, temp;
int* buffer;
int score_array[11];
for(iter = 0;iter < 11;++iter)
{
score_array[iter] = 0;
}
printf("How many grades will you be entering?\n");
printf("Enter a number between 1 and 100: ");
scanf("%d", &size);
buffer = (int*)malloc(size*sizeof(int));
for(iter = 1;iter <= size;++iter )
{
printf("Getting grade %d. You have %d grade(s) left to enter\n", iter, size-iter+1);
printf("Enter a number between 0 and 100: ");
scanf("%d",&temp);
if( (temp>=0) && (temp <= 100) )
{
buffer[iter-1] = temp;
}
else
{
do
{
printf("Invalid Value!\n");
printf("Getting grade %d. You have %d grade(s) left to enter\n", iter, size-iter+1);
printf("Enter a number between 0 and 100: ");
scanf("%d",&temp);
}
while( (temp < 0) || (temp > 100) );
}
}
for(iter = 1;iter <= size;++iter)
{
allocate(score_array, buffer[iter-1]);
}
for(iter = 0;iter < 11;++iter)
{
printf_star(score_array[iter]);
}
return 0;
}
I keep getting this error:
hw08.c: In function ‘main’:
hw08.c:56: warning: incompatible implicit declaration of built-in function ‘malloc’
This is only a warning, not an actual error, so the program still compiles.
To eliminate the warning you can declare the malloc in your file:
#include <stdio.h>
extern void * malloc(unsigned long);
You could also just include stdlib.h, unless you have a major reason not to.
Header files just define the functions prototypes by using the extern keyword. The actual implementation of malloc resides in libc depending on the OS.
Not defining a function/system call prototype is indeed a warning, not a compile-time error, contrary to what many have conveyed in the comments!
Coming to the actual workaround, if you want to avoid using the #include <stdlib.h>, you either need to use:
#include <malloc.h> (deprecated since c89)
Define the header all by yourself, with extern void * malloc(size_t);
Credits to #Chris Rouffer too! :)
You need to include stdlib.h if you want to access the malloc() function, because that is where it is defined. Otherwise the compiler doesn't know what to do.
You really are supposed to include the header in your code if you want to use the function, however, in theory you could just paste the implementation of malloc() in your source and then use it from there without the header. This is a bad idea however, since anybody looking at the code would expect malloc() to refer to the standard implementation defined in stdlib.h.

Calling Convention error - C

In the following code there is a calling convention error(possibly leading to an eternal loop), and i cannot detect it. I try to verify the code using 'Satabs'. What kind of model can bring the error to the surface. With the following model i get a segfault.
By changing the VLEN and TMAX you can play a bit.
Q1. What is the calling convention error?
Q2. What kind of model would be most appropriate to use for finding the error?
#include <stdio.h>
#if MODEL==1
#define VLEN 3
#define TMAX 4
int trans(int T,int*src,int*dst){
if (T < VLEN && T < TMAX && src[T] < 4){
dst[T]=src[T]+1;
return 1;
} else {
return 0;
}
}
#endif
struct next_state {
int next;
int src[VLEN];
};
typedef struct next_state *iterator_t;
void init(iterator_t iter,int *src){
for(int i=0;i<VLEN;i++){
iter->src[i]=src[i];
}
iter->next=0;
}
int next(iterator_t iter,int *dst){
#ifdef FIX_ARRAY
for(int i=0;i<VLEN;i++){
#else
for(int i=0;i<TMAX;i++){
#endif
dst[i]=iter->src[i];
}
int res=0;
while(!res&&iter->next<TMAX){
res=trans(iter->next,iter->src,dst);
iter->next++;
}
return res;
}
int find_depth(iterator_t iter,int *src){
int table[VLEN*TMAX];
int N=0;
init(iter,src);
for(int i=0;i<TMAX;i++){
if(next(iter,&(table[N*VLEN]))){
N++;
}
}
int depth=0;
for(int i=0; i<N;i++){
printf("Eimai stin for \n");
int tmp=find_depth(iter,&(table[i*VLEN]));
printf("tmp= %d\n",tmp);
if(tmp>=depth){
depth=tmp+1;
//assert(depth);
}
}
printf("\n\n");
return depth;
}
int main(int argc,char*argv[]){
int state[VLEN];
struct next_state ns;
for(int i=0;i<VLEN;i++){
state[i]=0;
}
int depth=find_depth(&ns,state);
printf("depth is %d\n",depth);
}
int depth=find_depth(&ns,state);
You are passing &ns, but taking arg in function as iterator_t iter, is this correct ?
void init(iterator_t iter,int *src){
for(int i=0;i<VLEN;i++){
iter->src[i]=src[i];
iter->src[i] is this expression fine?
I dont know 'Satabs' but the most promising candidate for an endless loop for me seems to be
while(!res&&iter->next<TMAX){
res=trans(iter->next,iter->src,dst);
iter->next++;
}
All other loops rather look like fix count.
This loop might be dangerous for itself even without the so called calling convention error, which doest jump to my eye yet.
Anyhow you should take a look not only to the call of the funtion trans but the whole call tree to it.
You could also try to paste your code there
http://gimpel-online.com//cgi-bin/genPage.py?srcFile=intro.txt&cgiScript=analyseCode.py&title=Introduction+and+Welcome&intro=Introducing+the+testing+facility&compilerOption=online32.lnt&in
Maybe you get a few more hints.
Just a guess:
Maybe 'Satabs' doesn't like undefined preprocessor conditions
like
#if MODEL==1

Referencing the values in pointers to arrays (c)

Note: Fixed (decription at bottom)
For some reason the following code:
(*p_to_array)[m_p->number_of_match_positions] = (*p_to_temp_array)[k];
where the types are:
match_pos_t (*p_to_array)[];
match_pos_t (*p_to_temp_array)[];
int number_of_match_positions;
int k;
BTW: match_pos_t is a struct:
typedef struct match_pos
{
char* string;
long match_position;
}match_pos_t;
causes a 'syntax error before '(' error'
This error does not occur if this code replaced with other code.
Could someone give me an idea of why this is causing a syntax error, and how I should fix this problem?
Entire relevant code:
typedef struct match_pos
{
char* string;
long match_position;
}match_pos_t;
typedef struct match_positions
{
int number_of_match_positions;
match_pos_t (*match_positions)[];
}match_positions_t;
typedef struct search_terms
{
int number_of_search_terms;
char* search_terms[];
}search_terms_t;
int BMH_string_search(char* search_string, char* file_string, match_positions_t* match_positions)
{
return 0;
}
int determine_match_pos(search_terms_t** s_terms, char* file, match_positions_t* m_p)
{
int i,j,k;
match_positions_t* temp_m_p;
i=0;
/* s_terms is a null terminated data structure */
while((*s_terms+i) != NULL)
{
for(j=0; j<(*s_terms+i)->number_of_search_terms; j++)
{
/* search for the string positions */
BMH_string_search((*s_terms+i)->search_terms[j], file, temp_m_p);
/* load out search positions into the return array */
if(temp_m_p->number_of_match_positions != 0)
{
int total_m_ps = m_p->number_of_match_positions + temp_m_p->number_of_match_positions;
m_p->match_positions = (match_pos_t (*)[])realloc(m_p->match_positions, sizeof(match_pos_t)*total_m_ps);
k = 0;
for( ; m_p->number_of_match_positions<total_m_ps; m_p->number_of_match_positions++)
{
(*(m_p->match_positions))[m_p->number_of_match_positions] = (*(temp_m_p->match_positions))[k];
k++;
}
}
free(temp_m_p);
}
i++;
}
return 0;
}
It appears I have been rather stupid. An extra set of parenthesis around the values being referenced does the trick (question code has been updated with fix):
Original:
(m_p->*match_positions)[m_p->number_of_match_positions] = (temp_m_p->*match_positions)[k];
Fixed:
(*(m_p->match_positions))[m_p->number_of_match_positions] = (*(temp_m_p->match_positions))[k];
If anyone has an explanation, though, about why the first is incorrect, rather than the second, it would be nice to hear though, as I thought that
object->*object2
was the same as
*(object->object2)
Is this correct or is there some c definitions that I am missing out on here?
I thought that object->*object2 was the same as *(object->object2)
No, in C, the . and -> operators expect an identifier as their right operand. The .* and ->* operators don't exist in C, you have to spell out *(structure.member) or *(structure_ptr->member) manually.

How can I pass an array as parameters to a vararg function?

I have some code that looks like this:
uint8_t activities[8];
uint8_t numActivities = 0;
...
activities[numActivities++] = someValue;
...
activities[numActivities++] = someOtherValue;
...
switch (numActivities)
{
0 : break;
1 : LogEvent(1, activities[0]); break;
2 : LogEvent(1, activities[0], activities[1]); break;
3 : LogEvent(1, activities[0], activities[1], activities[2]); break;
// and so on
}
where LogEvent() is a varargs function.
Is there a more elgant way to do this?
[Update] Aplogies to #0x69 et al. I omitted to say that there are many cases where LogEvent() could not take an array as a parameter. Sorry.
There's no standard way to construct or manipulate va_args arguments, or even pass them to another function (Standard way to manipulate variadic arguments?, C Programming: Forward variable argument list). You'd be better off seeing if you can access the internal routines of LogEvent.
pass a pointer to the array of ints and a number of ints instead
#include <stdio.h>
void logevent(int n, int num, int *l) {
int i;
for (i=0; i<num; i++) {
printf("%d %d\n",n,*(l++));
}
}
int main() {
int activities[8];
activities[0]=2;
activities[1]=3;
activities[2]=4;
int num=3;
int n=1;
logevent(n,num, activities);
printf("=========\n");
n=2;
activities[3]=5;
num=4;
logevent(n,num, activities);
}

How to use two parameters pointing to the same structure in one function?

I have my code below that consits of a structure, a main, and a function. The function is supposed to display two parameters that have certain values, both of which point to the same structure.
The problem I dont know how to add the SECOND parameter onto the following code :
#include<stdio.h>
#define first 500
#define sec 500
struct trial{
int f;
int r;
float what[first][sec];
};
int trialtest(trial *test);
main(){
trial test;
trialtest(&test);
}
int trialtest(trial *test){
int z,x,i;
for(i=0;i<5;i++){
printf("%f,(*test).what[z][x]);
}
return 0;
}
I need to add a new parameter test_2 there (IN THE SAME FUNCTION) using this code :
for(i=0;i<5;i++){
printf("%f,(*test_2).what[z][x]);
How does int trialtest(trial *test) changes ?
and how does it change in main ?
I know that I should declare test_2 as well, like this :
trial test,test_2;
But what about passing the address in the function ? I do not need to edit it right ?
trialtest(&test); --- This will remain the same ?
So please, tell me how would I use test_2 as a parameter pointing to the same structure as test, both in the same function..
Thank you !!
Please tell me if you need more clarification
I think that this is your homework, so I'll just write a different function that may give you an idea of what (I think) you need to do. I read that you don't want to change the trail_test parameter list, so I stuck with a similar parameter list.
struct thing {
/* put some stuff here */
};
typedef struct thing thing; /* because this is C, not C++ */
int how_many_things(thing * thing_list);
int main(void) {
int i;
thing * a;
int count_init = random(); /* let's surprise ourselves and make a random number of these */
count_init %= 128; /* but not too many or it might not work at all */
a = malloc(count_init*sizeof(things)+1);
for (i = 0; i < count_init; i++) {
thing_init(&(a[i]));
}
make_illegal_thing(&(a[count_init]) ); /* like '\0' at the end of a string */
printf("There are %i things in the list\n", how_many_things(a) );
return 0;
}
/* This is very similar to strlen */
int how_many_things(thing * a) {
int count = 0;
while (is_legal_thing(a) ) {
a++;
count++;
}
return count;
}

Resources