Thread: Variable arguments using an array code debug help?

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    13

    Variable arguments using an array code debug help?

    I'm not quite sure what I did wrong..It looks fine to me, but I get a segmentation fault.
    Code:
    #include <stdio.h>
    #define arrlen(x) (sizeof(x)/sizeof(*(x)))
    
    char stk[3][4] = { "arg","arg2","arg3" };
    char stk1[6][4] = { "arg","arg2","arg3","arg4","arg5","arg6" };
        
    int varfun(char stack[]){
        int i;
        for(i = 0; i < arrlen(stack); i++){
            printf("%s", stack[i]);
        }
        return 0;
    }
    
    
    
    int main(){
        varfun(stk);
        printf("----");
        varfun(stk1);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    Remember that arrays used as a function parameter are degraded to a pointer. In varfun stack is now *stack not stack[].

    Here's a link that explains this further:
    Question 6.21

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Actually, stack[] is perfectly valid way to declare a 1-d array function parameter. Question 6.21 is a different issue regarding the sizeof operator. You probably want to read Question 6.4. The issue is most likely with the arrlen() function that is incorrectly reporting the size, since there is no way to know where the end of the list is.

    Also, you need to print some newlines ('\n') or call fflush(stdout) periodically. stdout is line buffered, which means nothing shows up until you print a '\n' or call fflush(stdout) explicitly, which makes locating a seg fault via output very difficult since the seg fault may happen much later than the last thing you see printed on the screen.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    You may want to pass the size of the array in while it knows the length.

    Code:
    int main(){    
        varfun(*stk,arrlen(stk));
        printf("----");
        varfun(*stk1,arrlen(stk1));
        return 0;
    }
    Code:
    int varfun(char stack[],int stsz);

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Wow, I really didn't read that code very carefully. I totally missed the arrlen definition, and it's full of other bugs. You should have had several warnings/errors when you compiled. If not, you need to turn up the warnings on your compiler.


    • mnd22 was right as to the reason, though stack[] is still valid. stack is effectively a pointer-to-char, which for which your arrlen wont work (it gives you sizeof(char *) / sizeof(char) regardless of number of elements), hence the seg fault.
    • FloridaJo is also right, you need to calculate the length somewhere where the actual stk and stk1 arrays are visible, not the parameter they get passed into, for the reason above.
    • Your arrays are not declared properly. "arg2", "arg3", etc require 5 bytes. 4 for the letters and number, and one for the null. Make them stk[3][5] and stk1[6][5].
    • Your varfun is declared improperly. You need to pass in an array of char arrays, like so: int varfun(char stack[][5], int size). Then, calculate and pass in the size in main as FloridaJo suggested.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  2. pass on variable arguments
    By Fader_Berg in forum C Programming
    Replies: 5
    Last Post: 09-11-2009, 09:04 AM
  3. variable arguments in functions
    By Stonehambey in forum C++ Programming
    Replies: 11
    Last Post: 02-22-2008, 05:10 AM
  4. How slow are variable arguments?
    By dwks in forum C Programming
    Replies: 5
    Last Post: 09-18-2005, 12:11 PM
  5. (Variable Arguments,...)
    By Sebastiani in forum C Programming
    Replies: 3
    Last Post: 09-07-2001, 10:44 AM