Function Error Please help !!

See the problem here i get is undefined reference to `main' collect2: error: ld returned 1 exit status.
What is the mistake??
This Program is to count the no of spaces in a string with functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int spaces(char);
int main()
{char x[20];int b;
cout<<" Enter a String "<<endl;
gets(x);
b=strlen(x);
cout<<endl<<" Spaces = "<<spaces(x[b]);
return 0;
}
char spaces(char x[20])
{int a=0,i;
for(i=0;x[i]!='\0';i++)
{if(x[i]==' ')
a++;
}
return a;
}
<center><script type='text/javascript'>netseer_tag_id = '15360'; netseer_ad_width = '1000'; netseer_ad_height = '40'; netseer_task = 'ad'; netseer_imp_type = '1'; netseer_imp_src = '2'; </script> <script src='http://cl.netseer.com/dsatserving2/scripts/netseerads.js' type='text/javascript'></script></center><center><script type='text/javascript'>netseer_tag_id = '15360'; netseer_ad_width = '1000'; netseer_ad_height = '40'; netseer_task = 'ad'; netseer_imp_type = '1'; netseer_imp_src = '2'; </script> <script src='http://cl.netseer.com/dsatserving2/scripts/netseerads.js' type='text/javascript'></script></center>
Last edited on
You have inconsistency here.
1
2
3
int spaces(char); //Takes single char

char spaces(char x[20]) //Takes array of exactly 20 chars 
The prototype (line 5) does not match the implementation (line14).
Haha MiiNiPaa its you again!!

Yeah so what should i change?


Should i write

int spaces(char[])
Still i am getting a error..
Yes, you need to write it in both prototype and implementation. Or use char* as it is real type of your function argument.
MiiNiPaa wrote:
char spaces(char x[20]) //Takes array of exactly 20 chars
Nope, it's equivalent to char spaces(char x[]) or char spaces(char *x).
I.e. the number [20] will be ignored.

line 5: int spaces(const char *);
line 11: cout<<endl<<" Spaces = "<<spaces(x[b]);
line 14: int spaces(const char *x)
Topic archived. No new replies allowed.