Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
Hi all. I have a problem concerning a function call that is not liking my code apparently

For some reason I can not get my method stubs to work out.


I have it set up like this

for example say I have a function




void function1(char string1[])
{

various commands

int x=function2(); //this is a function call to store x as result of function2()

more commands

}


then i have



int function2( char string1[] )
{

int x=atoi("%s",string1);
return x;

}



is this the right method to do a stub? When I try to compile this it can not get by the function call int x=function2();

the error meassage is "too few arguements in function a^"




Thanks for any insight on this!
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
In your function argument list, you have a character array specified, therefore you must pass one to the function when you call it:

int x = function2("My String");


You need to match the number and types of arguments to the function description.
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
how does one access the characters in a string. I am trying to encode a line one line at a time encoding each character the line has.

so far i have

while(fgets(row,row[100],sourcefile!=NULL)
{
code(orig,encrypted);
fputs(row,returnfile);
}


fclose(sourcefile);
fclose(returnfile);


}


now the function code is

void code(charorig[], char encrypted[] )
{
char variable;
int variable1;
int i;

variable=row;

variable2=(int)variable/10

}


thanks, ive been trying to figure this out for pretty much this whole afternoon to no avail lol
so any insight would be great
 

PrOeliuM

macrumors member
Jul 31, 2005
30
0
If your storing the string as a character array, then you can use an array index to access the individual characters. For instance: char mystring[10] = "macpro";

mystring[0] = 'm'
mystring[1] = 'a'
...

In your function code (if what you posted is your actual function), you declare 'i' and then reference row and place that value into 'variable'. This is hazardous because you don't know what 'i' is when you're referencing row. Define your 'i' variable to be what you want, and then go about referencing the 'row' array.
 

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,160
4,152
5045 feet above sea level
PrOeliuM said:
If your storing the string as a character array, then you can use an array index to access the individual characters. For instance: char mystring[10] = "macpro";

mystring[0] = 'm'
mystring[1] = 'a'
...

In your function code (if what you posted is your actual function), you declare 'i' and then reference row and place that value into 'variable'. This is hazardous because you don't know what 'i' is when you're referencing row. Define your 'i' variable to be what you want, and then go about referencing the 'row' array.



Thanks but I think I am still confused. Pretty much what I am trying to do is read in a file that has multiple lines in the file.

Then I want to encode each character of that line using a math function and output the result to in this case return file
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
Remember that C arrays are just pointers with fancy syntax. A function doesn't know the difference. If you really need a function to know the dimensions of an array, you have to pass that to the function too. Strings coming in from fgets() give us the usual nul terminator, so we don't have to worry about counts here.

Code:
#include <stdio.h>
#include <string.h>


/* Apply unbreakable xor42 encryption to the string */
void
code(char *orig, char *encrypted)
{
    int i;

    /* loop until we hit the '\0' terminator */
    for (i=0; orig[i]; i++) {
        encrypted[i] = orig[i] ^ 42;
    }
    /* add the string terminator now */
    encrypted[i] = 0;
}


int
main()
{
    FILE *sourcefile, *returnfile;
    char row[100], encrypted[100];

    sourcefile = fopen("junk.txt", "r");
    returnfile = fopen("newjunk.txt", "w");

    while(fgets(row, 100 ,sourcefile) !=NULL) {
        code(row, encrypted);
        fputs(encrypted,returnfile);
    }

    fclose(sourcefile);
    fclose(returnfile);

    return 0;
}

[ edit: You could write this line:
Code:
code(char *orig, char *encrypted)
as
Code:
code(char orig[], char encrypted[])
and the innards of the function would remain the same. ]
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.