I have a C++ program that shows an error:
too few arguments to function void split(char*, char**, int, int, int*)
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
void split(char* lin, char** word, int i, int w, int* c);
int main() {
char line[80] = "myline";
int n = 5;
char **word;
split(line, word, 1, 1); //Error is here.
return 0;
}
void split(char* lin, char** word, int i,int w, int* c)
{
//statements
}
Can anyone tell whats wrong?
The function split takes 5 arguments and no default argument. You try to call it with 4 arguments. That wont work.
The last two times you call split() you're calling it with only 4 arguments, as in one too few. If you'd like you can define it for 4 arguments as well, but currently this is not the case
Related
I'm working with C, and I'm trying to build a kind of "composite" function, by joining 2 functions with the same signature, and a third with almost the same signature but just one less argument. The functions should be executed in sequence, but the final function must have the same signature. You can think it as building a function with code blocks using same signature(I have it implemented in C++ with policy class, but I'm trying a C approach as the rest of the code is in C already).
I built some code very simple, just to explain my approach.
#include <stdio.h>
#include <stdlib.h>
typedef void simulFileProc(int a, int b);
typedef void simulRead(int a);
typedef struct compFunct{
simulFileProc* file1;
simulRead* read;
simulFileProc* file2;
} compFunct;
void realProc(int a, int b){
printf("call from realProc %d, %d\n",a,b);
}
void realRead(int a){
printf("call from read %d\n",a);
}
simulFileProc* join(int a, int b, compFunct* func){
void sf(int a, int b){
func->file1(a,b);
printf("intermediate line\n");
func->read(a);
func->file2(a,b);
}
return &sf;
}
int main() {
compFunct* c = malloc(sizeof(256));
c->file1 = &realProc;
c->read = &realRead;
c->file2 = &realProc;
int a=0;
int b=0;
simulFileProc* s = join(a,b,c);
s(4,3);
return 0;
}
It is working, but for some reason, just the first function print.
call from realProc 4, 3
intermediate line
If I comment the line "func->read(a);", I have a segmentation fault.
What is wrong ?? Is there a smarter way to do ?
I am new to the concept of character pointers and strings.So, having trouble in passing a string pointer and returning a string pointer in a function.the function is "remove".
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* remove(char* ,char ,int );
int main(){
int count, i;
char ch;
char* a;
char* b;
b=(char*)malloc(sizeof(char)*10);
a=(char*)malloc(sizeof(char)*10);
gets(a);
for(i=0;*a='\0';i++)
{
ch=a[i];
b=remove(&a[i],ch,i);
}
}
char* remove(char* str,char x,int k)
{
char* str2=(char*)malloc(sizeof(char)*10);
int i,j;
j=0;
int len;
len=strlen(str);
for(i=k;i<len;k++)
{
if(str[i]!='x')
{
str2[j]=str[i];
j++;
}
}
return str2;
}
The errors which i am getting are
error:conflicting types for 'remove'
In the line where the function was declared and defined in the subsequent line.
Function remove() is already defined in stdio.h.
int remove(const char *pathname);
Please give some other name to your function char* remove().
Note : Whenever you get such error, please try to look at manual page. If you are in unix. Simply type man func_name in terminal.
remove() is a standard function. You need to choose a different name for your remove() function name. Because your function definition for remove() differs with the prototype found in stdio.h.
Can you guys help me on function m? The idea is to printf the "tab", but i don't understand what is wrong
#include <stdio.h>
#define MAXL 50
#define MAXC 50
unsigned int linhas;
unsigned int colunas;
int segC [MAXL];
int segL [MAXC];
char tab[MAXL][MAXC];
void c (){
int l,c,temp;
scanf("%d %d",&linhas,&colunas);
for (l=0;l<linhas;l++){
scanf("%d[^'']",&temp);
segC[l]=temp;
}
for (c=0;c<colunas;c++){
scanf("%d[^'']",&temp);
segC[c]=temp;
}
for(l=0;l<=linhas;l++){
for(c=0;c<colunas;c++){
scanf("%c",&tab[l][c]);
}
}
}
char m (linhas,colunas,segC,segL,tab){
int l,c;
int tempi;
char tempc;
for(l=0;l<=linhas;l++){
for(c=0;c<colunas;c++){
printf("%c",tab[l][c]);
}
tempi=segL[l];
printf("%d\n",tempi);
}
for(c=0;c<colunas;c++){
tempi=segC[c];
printf("%d",tempi);
}
printf("\n");
}
char h (int line){
}
int main (){
c();
//m(linhas,colunas,segC,segL,tab);
}
Rewrite the function like this:
char m() {
/* ... */
}
You do not need to provide global variables as arguments to a function; in fact, the local function parameters shadow the global variables.
Finally, avoid omitting parameter and variable types; that is at the very least deprecated or even illegal as of C99 (omitted types default to int which is causing the problem here.)
Better yet, declare them as local variables in main() and pass them by pseudo-reference to both m() and c():
char m( unsigned int linhas, unsigned int colunas, int **segC, int **segL, char ***tab ) {
/* ... */
}
Pass the address of segC, segL, and tab when calling.
You're missing variable types:
char m (linhas,colunas,segC,segL,tab)
This procedure should convert a string that contains a set of double numbers separated by comma (e.g. 7.2,9.5,-5.515) to a vector of double type.
void ToDoubleVec(int d,const char* commaSeparated,double *result)
{
int i;
result[0]=atof(strtok(commaSeparated,","));
for(i=1;i<d;i++)
result[i]=atof(strtok(NULL,","));
}
Here is the snippet of program that calls it:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc,char** argv)
{
...
int i,dim=atoi(argv[1]);
double *lower;
lower = malloc(dim*sizeof(double));
ToDoubleVec(dim,argv[2],lower);
...
}
Debugger's output:
40 lower = malloc(dim*sizeof(double));
(gdb) s
42 ToDoubleVec(dim,argv[2],lower);
(gdb) s
ToDoubleVec (d=2, commaSeparated=0x7fffffffe9d3 "2.3,-62.1", result=0x603010) at testPSO.c:11
11 result[0]=atof(strtok(commaSeparated,","));
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff77f56bb in ?? () from /lib/x86_64-linux-gnu/libc.so.6
Why doesn't it work? I was sure that I've allocated enough memory for the array and also parameters seems to be passed correctly.
You can reduce your code to this SSCCE (Short, Self-Contained, Correct Example), which crashes nicely when you leave out #include <string.h> and does not compile cleanly when you add #include <string.h>:
segv.c: In function ‘ToDoubleVec’:
segv.c:8:5: warning: implicit declaration of function ‘strtok’ [-Wimplicit-function-declaration]
segv.c:8:20: warning: initialization makes pointer from integer without a cast [enabled by default]
segv.c:14:20: warning: assignment makes pointer from integer without a cast [enabled by default]
Code:
#include <stdlib.h>
//#include <string.h>
static void ToDoubleVec(int d, const char* commaSeparated, double *result)
{
int i;
result[0] = atof(strtok(commaSeparated, ","));
for (i = 1; i < d; i++)
result[i] = atof(strtok(NULL, ","));
}
int main(void)
{
int dim = 2;
double *lower = malloc(dim*sizeof(double));
char arg[] = "7.2,9.5,-5.515";
ToDoubleVec(dim, arg, lower);
}
Passing the return value from a function such as strtok() which can return a null pointer directly to a function such as atof() which does not tolerate null pointers is foolhardy; it leads to crashes. If everything is correct, you'll be OK; if not, you'll crash and burn.
The unchecked memory allocation is a similar problem; you didn't even check that dim was non-zero (and non-negative) before doing the memory allocation in the original.
#include <assert.h>
#include <string.h>
#include <stdlib.h>
static void ToDoubleVec(int d, char *commaSeparated, double *result)
{
int i;
char *number = strtok(commaSeparated, ",");
if (number != 0)
{
result[0] = atof(number);
for (i = 1; i < d; i++)
{
number = strtok(NULL, ",");
if (number != 0)
result[i] = atof(number);
}
}
}
int main(void)
{
int dim = 2;
double *lower = malloc(dim*sizeof(double));
char arg[] = "7.2,9.5,-5.515";
assert(lower != 0);
ToDoubleVec(dim, arg, lower);
}
You could — and in one version of the code I did — add error printing to report if the tests on number failed. But the crash is caused by the implicit declaration of strtok() as returning int and not char *.
I have tried to compile your code, and the compiler warned me that strtok() takes as input a char* and not a const char*. Then I have tried this code, and it is working correctly:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void ToDoubleVec(int d, char* commaSeparated,double *result);
int main(int argc,char** argv)
{
int i,dim=atoi(argv[1]);
double *lower;
lower = malloc(dim*sizeof(double));
ToDoubleVec(dim,argv[2],lower);
for (i=0; i<dim; ++i) {
printf("%f\n", lower[i]);
}
return 0;
}
void ToDoubleVec(int d, char* commaSeparated,double *result)
{
int i;
result[0]=atof(strtok(commaSeparated,","));
for(i=1;i<d;i++)
result[i]=atof(strtok(NULL,","));
}
So try to change const char* to char*, and check the input you pass to your program, maybe it is not correct and this could be the problem.
I am reading an article about code obfuscation in C, and one of the examples declares the main function as:
int main(c,v) char *v; int c;{...}
I've never saw something like this, v and c are global variables?
The full example is this:
#include <stdio.h>
#define THIS printf(
#define IS "%s\n"
#define OBFUSCATION ,v);
int main(c, v) char *v; int c; {
int a = 0; char f[32];
switch (c) {
case 0:
THIS IS OBFUSCATION
break;
case 34123:
for (a = 0; a < 13; a++) { f[a] = v[a*2+1];};
main(0,f);
break;
default:
main(34123,"#h3eglhl1o. >w%o#rtlwdl!S\0m");
break;
}
}
The article: brandonparker.net (No longer works), but can be found in web.archive.org
It's the old style function definition
void foo(a,b)
int a;
float b;
{
// body
}
is same as
void foo(int a, float b)
{
// body
}
Your case is same as int main(int c,char *v){...} But it's not correct.
The correct syntax is : int main(int c, char **v){...}
Or, int main(int c, char *v[]){...}
EDIT : Remember in main() , v should be char** not the char* as you have written.
I think it's K & R C style.
It is a pre-ANSI C syntax for function declaration. We don't use it anymore. It is the same as:
int main(int c, char *v)