Thread: Accessing consecutively numbered arrays

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    6

    Accessing consecutively numbered arrays

    This may be a long shot, but anyway... Say I have lots of arrays named list1[], list2[], list3[], etc. I want a while() loop or something similar where I do something like this:
    Code:
     while(1) 
    {    afunction(listX);
     }
    ....where X is incremented each time.
    Is there a way to do this? I'm using GCC.


    In case you wonder why I want to do this, I'm saving C arrays from Wireshark to feed into a C function for testing and the arrays it generates have incrementing numbers. I'm simply copy and pasting them, as-is, into my C file. The final target for the code is a small ARM processor, but I want to test my C function on a computer first so I don't care about code efficiency in this case.

    Changing the syntax isn't really acceptable because I may have 100 of these arrays and it's very tedious to do this.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Can you make all of those separate one-dimensional arrays into a single two-dimensional array? You could then loop through them much easier.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Quote Originally Posted by hk_mp5kpdw View Post
    Can you make all of those separate one-dimensional arrays into a single two-dimensional array? You could then loop through them much easier.

    Well yes....but that means I have to reformat all the arrays. That's what I'm trying to avoid.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Do you have something like this?
    char a1[100];
    char a2[100];
    char a3[100];

    Are all your arrays the same type and length?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Quote Originally Posted by Salem View Post
    Do you have something like this?
    char a1[100];
    char a2[100];
    char a3[100];

    Are all your arrays the same type and length?
    Yes, yes, and no.

    All type char, but not the same length unfortunately.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So you have an array of char * and each element points to one of your different arrays:
    Code:
    char a1[17];
    char a2[42];
    char a3[100];
    
    char *array_or_arrays[3] = {a1, a2, a3};

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Note that you'll have to make the a1, a2, etc arrays global (or static).
    Code:
    #include <stdio.h>
    
    char a1[100];
    char a2[100];
    char a3[100];
    
    int main(void) {
        char *a[] = {a1,a2,a3};
        printf("%d\n", a[1][5]);
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by oogabooga View Post
    Note that you'll have to make the a1, a2, etc arrays global (or static).
    No, it will work if they're local, as long as they're in the same scope as, or an outer scope of, a. They simply must be valid whenever you're accessing them through a.
    Last edited by anduril462; 03-08-2012 at 11:08 AM. Reason: fixed broken tag

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If I put the arrays inside main (and not static), I get the following warning (3 times on line 8 above):
    [Warning] initializer element is not computable at load time

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by oogabooga
    If I put the arrays inside main (and not static), I get the following warning (3 times on line 8 above):
    Then populate array_or_arrays element by element. Declaring the arrays global (or static) should be a result of other concerns, not the concern that the syntax for initialising array_or_arrays results in a compile warning. (EDIT: a quick test with gcc shows that this is probably a matter of C89 versus C99.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    a quick test with gcc shows that this is probably a matter of C89 versus C99.)
    Really?

    Code:
    #include <stdio.h>
    
    int main(int argc, const char *argv[]) {
    	char list1[100], list2[100], list3[50],
    		*p[3] = { list1, list2, list3 };
    	return 0;
    }
    root~/C»gcc -Wall test.c
    test.c: In function ‘main’:
    test.c:5:4: warning: unused variable ‘p’
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try:
    Code:
    gcc -Wall -pedantic -ansi test.c
    versus:
    Code:
    gcc -Wall -pedantic -std=c99 test.c
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    pendantry.

    If you can't use C99, this will work:

    Code:
    #include <stdio.h>
    
    int main(int argc, const char *argv[]) {
    	char list1[100], list2[100], list3[50],
    		*p[3];
    	
    		p[0] = list1;
    		p[1] = list2;
    		p[2] = list3;
    
    	return 0;
    }
    No warnings or errors with "gcc -Wall -pedantic -ansi".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Quote Originally Posted by anduril462 View Post
    So you have an array of char * and each element points to one of your different arrays:
    Code:
    char a1[17];
    char a2[42];
    char a3[100];
    
    char *array_or_arrays[3] = {a1, a2, a3};
    Ok I tried this, by using Excel to create a list of numbers and using copy and paste tricks with my editor. Now I have another problem. I failed to mention that the size of each of these arrays is required. Normally I'd use sizeof, but this doesn't seem to work with pointers.

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Just make a parallel array with the sizes.
    Code:
    int sizes[] = {17, 42, 100};

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help...accessing character arrays in a structure
    By mathewmc in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 11:20 AM
  2. Accessing Arrays
    By perkins in forum C++ Programming
    Replies: 7
    Last Post: 12-30-2005, 06:06 PM
  3. numbered lines
    By michele CD in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2003, 11:44 AM
  4. accessing arrays with pointers
    By Nutka in forum C Programming
    Replies: 9
    Last Post: 09-30-2002, 08:19 PM
  5. Calling WSASend() Consecutively :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-17-2002, 09:30 AM