Looping through multiple arrays in C.

Tags
c programming, multiple arrays, programming, solved

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 08-07-2018
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?
This User Gave Thanks to Azrael For This Post:
Neo (08-07-2018)
# 2  
Old 08-07-2018
Did you consider making c a pointer to int array?
This User Gave Thanks to RudiC For This Post:
Azrael (08-07-2018)
# 3  
Old 08-07-2018
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 08-07-2018
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; 08-07-2018 at 06:19 PM.. Reason: fixed several errors
These 3 Users Gave Thanks to Corona688 For This Post:
Azrael (08-08-2018), Peasant (08-08-2018), RudiC (08-07-2018)
# 5  
Old 08-08-2018
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!
This User Gave Thanks to Azrael For This Post:
Corona688 (08-16-2018)
Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Creating script with multiple job arrays idbemad Shell Programming and Scripting 3 2 Weeks Ago 06:19 AM
Looping an array of 2d arrays in C Azrael Programming 3 09-18-2018 11:26 AM
Curl - upload multiple attachment arrays to server webpage jaango123 Shell Programming and Scripting 0 04-16-2017 12:41 PM
Compare multiple arrays elements using awk alex2005 Shell Programming and Scripting 2 11-26-2016 06:27 AM
Multiple arrays in variable using for loop ratheeshp Shell Programming and Scripting 1 06-13-2016 01:00 PM
Loop over multiple arrays samsan Shell Programming and Scripting 2 09-03-2014 02:52 AM
Looping all subdierctories in multiple pipes danbroz Shell Programming and Scripting 2 05-15-2014 12:16 PM
awk arrays comparing multiple columns across two files. hubleo Shell Programming and Scripting 4 01-02-2014 01:28 PM
Looping through arrays newbie2010 Shell Programming and Scripting 4 04-27-2013 04:12 PM
Looping for multiple directories guns Shell Programming and Scripting 7 11-07-2011 06:18 AM
multiple looping with case and funtion showing error, Please help Renjesh Shell Programming and Scripting 5 09-19-2011 09:33 AM
Help in Array looping and creating multiple lines sexyTrojan UNIX for Dummies Questions & Answers 0 11-02-2009 08:30 PM
Read multiple arrays in mysql pinkgladiator Shell Programming and Scripting 0 11-10-2008 11:19 PM
multiple arrays through awk... newbeenie UNIX for Advanced & Expert Users 2 08-17-2006 02:48 PM
Nested Loop to Echo Multiple Arrays yongho Shell Programming and Scripting 1 07-12-2005 12:35 PM
All times are GMT -4. The time now is 07:15 AM.

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





Not a Forum Member?
Forgot Password?