Function Error Please help !!
Jan 19, 2015 at 4:04pm UTC
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 Jan 19, 2015 at 4:09pm UTC
Jan 19, 2015 at 4:23pm UTC
You have inconsistency here.
1 2 3
int spaces(char ); //Takes single char
char spaces(char x[20]) //Takes array of exactly 20 chars
Jan 19, 2015 at 4:27pm UTC
The prototype (line 5) does not match the implementation (line14).
Jan 19, 2015 at 4:28pm UTC
Haha MiiNiPaa its you again!!
Yeah so what should i change?
Should i write
int spaces(char [])
Jan 19, 2015 at 4:30pm UTC
Still i am getting a error..
Jan 19, 2015 at 4:31pm UTC
Yes, you need to write it in both prototype and implementation. Or use char *
as it is real type of your function argument.
Jan 20, 2015 at 8:49am UTC
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.