calling a function

Discussion in 'Mac Programming' started by dukebound85, Oct 18, 2006.

  1. dukebound85 macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #1
    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!
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    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.
     
  3. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
  4. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #4
    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
     
  5. PrOeliuM macrumors member

    Joined:
    Jul 31, 2005
    #5
    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.
     
  6. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #6



    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
     
  7. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #7
    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. ]
     

Share This Page