👤
Home Man
Search
Today's Posts
Register

Post questions about C, C++, Java, SQL, and other programming languages here.

Looping through multiple arrays in C.

Tags
c programming, multiple arrays, solved

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
Looping through multiple arrays in C.

Not sure if this is possible, but I've tried this about a thousand ways now. I am making something with a lot of arrays. I thought I could put the array names into a separate array and then loop through them to call all of their elements. This is the best I've got so far:

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

int main() {

	char c;

	int a [5] = {1,2,3,4,5};
	int b [5] = {6,7,8,9,10};

	for(c = 'a'; c <= 'b'; ++c) {
		for (int j = 0; j < 5; j++){
			printf("%d\n", c[j]);
		}
	}
}

From it I get the following error:
Code:
$ gcc canloop.c -o canloop
canloop.c: In function ‘main’:
canloop.c:17:20: error: subscripted value is neither array nor pointer nor vector
    printf("%d\n", c[j]);

Oddly enough it still runs despite the error:
Code:
$ ./canloop 
0
1
2
3
4
0
1
2
3
4

Seems its not incrementing on the first loop with c. I've tried changing ++c to c++, casting, etc, but no change. Anyone know if this is not possible, some way to do this better or what I may be missing?
The Following User Says Thank You to Azrael For This Useful Post:
Neo (1 Week Ago)
# 2  
Old 1 Week Ago
Did you consider making c a pointer to int array?
The Following User Says Thank You to RudiC For This Useful Post:
Azrael (1 Week Ago)
# 3  
Old 1 Week Ago
I did try that with pointers several different ways. Just tried it again and for the first time this compiled with no errors:

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

int main() {

    int c;
    int *test;
    test = &c;

    int a [5] = {1,2,3,4,5};
    int b [5] = {6,7,8,9,10};

    for(c = 'a'; c <= 'b'; ++c) {
        for (int j = 0; j < 5; j++){
            printf("%d\n", test[j]);
        }
    }
}

The output is uniq, but wrong:

Code:
$ ./canloop
97
757351276
32767
0
4
98
757351276
32767
0
4

I guess I'm not dereferencing it right? Maybe I've just been up too long, but I'm open to more suggestions.
# 4  
Old 1 Week Ago
That it compiled without errors only means your program is correct grammatically, the same way "my hovercraft is full of eels" will pass a grammar check but not help Belgians communicate with foreigners. Your program does not do what you think it does.

Code:
for(c = 'a'; c <= 'b'; ++c)

'a' and 'b' are the ASCII integers 91 and 92, respectively. They are not the arrays a and b and cannot be used to retrieve those variables.

How about this?

Code:
#include <stdio.h>

int main(int argc, char *argv[])
{
        int a [5] = {1,2,3,4,5};
        int b [5] = {6,7,8,9,10};
        int *c[]={a, b}; // c[0] is effectively a, c[1] is effectively b

        int n, m;

        for(n=0; n<1; n++)
        {
                for(m=0; m<5; m++)
                {
                        printf("Array %d[%d] = %d\n", n, m, c[n][m]);
                }
        }
}


Last edited by Corona688; 1 Week Ago at 05:19 PM.. Reason: fixed several errors
The Following 3 Users Say Thank You to Corona688 For This Useful Post:
Azrael (1 Week Ago), Peasant (1 Week Ago), RudiC (1 Week Ago)
# 5  
Old 1 Week Ago
I will agree with what you said. I knew it was a logical error instead of syntactical one when I saw it. I was just happy to have made any progress at that point.

Your code is much better and cleaner, but I did have to edit it. At first it only printed the first array:

Code:
$ ./canloop 
Array 0[0] = 1
Array 0[1] = 2
Array 0[2] = 3
Array 0[3] = 4
Array 0[4] = 5

I changed 'for(n=0; n<1; n++)' to 'for(n=0; n<2; n++)' and both arrays printed correctly:

Code:
 $ ./canloop 
Array 0[0] = 1
Array 0[1] = 2
Array 0[2] = 3
Array 0[3] = 4
Array 0[4] = 5
Array 1[0] = 6
Array 1[1] = 7
Array 1[2] = 8
Array 1[3] = 9
Array 1[4] = 10

Much appreciated!
👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare multiple arrays elements using awk alex2005 Shell Programming and Scripting 2 11-26-2016 05:27 AM
Multiple arrays in variable using for loop ratheeshp Shell Programming and Scripting 1 06-13-2016 12:00 PM
Loop over multiple arrays samsan Shell Programming and Scripting 2 09-03-2014 01:52 AM
Looping all subdierctories in multiple pipes danbroz Shell Programming and Scripting 2 05-15-2014 11:16 AM
Looping through arrays newbie2010 Shell Programming and Scripting 4 04-27-2013 03:12 PM
Looping for multiple directories guns Shell Programming and Scripting 7 11-07-2011 05:18 AM
multiple looping with case and funtion showing error, Please help Renjesh Shell Programming and Scripting 5 09-19-2011 08:33 AM
Help in Array looping and creating multiple lines sexyTrojan UNIX for Dummies Questions & Answers 0 11-02-2009 07:30 PM
Read multiple arrays in mysql pinkgladiator Shell Programming and Scripting 0 11-10-2008 10:19 PM
multiple arrays through awk... newbeenie UNIX for Advanced & Expert Users 2 08-17-2006 01:48 PM


All times are GMT -4. The time now is 09:57 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password