int value passed to function

This is a discussion on int value passed to function within the C Programming forums, part of the General Programming Boards category; hey guys this is probably an easy question to answer, but i just dont get it. i have a main ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    int value passed to function

    hey guys
    this is probably an easy question to answer, but i just dont get it.

    i have a main method where i call a function parse: something like that:

    Code:
    int main()
    {
    char *newargv[MAXITEM];
    int size;
    parse(newargv, size);
    printf("argvsize = %i\n", newargvSize);
    }
    
    int parse(char *newargv[], int newargvSize)
    {
    newargvSize = 0;
    
    // loop where values are assigned to the newargv array and the size is increaded
    // newargv[newargvSize] = some string;
    // newargvSize++;
    
    printf("newargvSize: %i\n", newargvSize);
    }
    now the printf statement in my parse method outputs the correct value, where as my printf after the function call returns some random value (an address i guess). what would i have to do to get the same value as in my function. i tried to work with pointers and stuff, but couldn't figure it out yet!

    any help appreciated,
    thx

  2. #2
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    You need to pass a pointer.

    This is a call by value.

    The local copy of newargvSize is lost after going out of the function.

    Also, your int function doesnt return anything. You might want to return newargvSize to a variable inside main. That should work if you dont want to use pointers.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Code:
    int parse(char *newargv[], int newargvSize)
    {
    newargvSize = 0;
    
    // loop where values are assigned to the newargv array and the size is increaded
    // newargv[newargvSize] = some string;
    // newargvSize++;
    
    printf("newargvSize: %i\n", newargvSize);
    }
    Won't this perform an 'infinite' loop?
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    thanks for this fast reply!

    yeah i'm returning a different value already, because i call another function in my parse function, forgot to mention that.

    i tried to do it with pointers. i just don't get it i guess. heres what i had:

    Code:
    // main method
    int newargvSize;
    returnValue = parse(newargv, &newargvSize);
    
    // parse method
    int parse(char *newargv[], int *newargvSize)
    {
    newargvSize = 0;
    newargv[newargvSize] = some string; // this results into the following error: array subscript is not an integer
    newargvSize++;
    }
    so i created a new int size = 0;
    used that in the array -> newargv[size] = some string; and tried to assign size to newargvSize, but that didnt work either. What am i doing wrong here? i have a feeling its not much, but i have no clue right now

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    well here is my whole parse method, to make it a little easier to understand i guess:

    Code:
    int parse(char *newargv[], int *newargvSize)
    {
        char word[STORAGE];
        int returnValue = 0;
        newargvSize = 0;
    
        //int size = 0;
    
        for (;;)
        {
            returnValue = getword(word);
    
             //printf("pointer: %i\n", *newargvSize);
    
            if (returnValue == 0 || returnValue == -1)
            {
                newargv[newargvSize] = NULL;
                break;
            } else
            {
                // copy the word to the array because it will be overwritten
                // each time the loop is run again
                newargv[newargvSize] = (char*) malloc(strlen(word) + 1);
                strcpy(newargv[newargvSize], word);
                newargvSize++;
            }
        }
    
       // newargvSize = *size;
    
        //printf("size: %i\n", size);
        printf("newargvSize: %i\n", newargvSize);
        //printf("pointer: %i\n", *newargvSize);
    
        return returnValue;
    }

  6. #6
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Code:
    int main()
    {
    char *newargv[MAXITEM];
    int size;
    printf("argvsize = %i\n", newargvSize = parse(newargv, size););
    }

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    hmm.. thx, but i'm not returning the size (i can't) isn't that what your code snippet does?

    the way i have my function now with the int pointer, is that how its supposed to be?
    how do i use my int value in the array then?

    newargv[newargvSize] = (char*) malloc(strlen(word) + 1);

    this doesnt work anymore as a pointer. always get the error message -> array subscript is not an integer

    what am i missing? :s

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Yep, you're nearly there!

    Code:
    newargv[newargvSize] = (char*) malloc(strlen(word) + 1);
    Quote Originally Posted by search
    always get the error message -> array subscript is not an integer
    Quite right, it is not an integer. It's a pointer, i.e. an address. You need to dereference the pointer. The dereferencing operator in C is *, and you need to use it whenever you want to access the value pointed to. So you should access the array like:

    Code:
    newargv[*newargvSize] = (char*) malloc(strlen(word) + 1);
    Assign to newargvSize in parse like
    Code:
    *size = 0;
    The rest of it looks correct, if you can't return the size.

    Code:
    int parse(char *newargv[], int *newargvSize)
    Code:
    // main method
    int newargvSize;
    returnValue = parse(newargv, &newargvSize);
    
    printf("newargvSize = %i", newargvSize); // no * needed here, newargvSize is not a pointer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21