Home Man
Search
Today's Posts
Register

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

Looping an array of 2d arrays in C

Tags
array of 2d arrays, c programming, looping, segfault, solved

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 4 Days Ago
Looping an array of 2d arrays in C

Le sigh... Hopefully this will be the last time I have to ask for help on this topic. For a while now I've been working with a 1d array that holds 2d arrays. For reference you can view here. Now I'm just trying to loop through the elements with the following:

Code:
#include <stdio.h>

void arrbyptr(int (*ptr[2][2])[2]) {

int result;

    for ( int i = 0; i < 2; ++i ){
        for ( int j = 0; j < 2; ++j ){
            for ( int k = 0; k < 2; ++k ){
                result = (*ptr[i][j])[k];
                printf("%d\n", result);
            }
        }
    }
}

int main(void)
{
        int arr2d[2][2]={ {1,2},{3,4} };
        int arr2db[2][2]={ {5,6},{7,8} };

        int (*ptr[2][2])[2]= { { arr2d, arr2db } };

        arrbyptr(ptr);
}

It runs momentarily before segfaulting:

Code:
$ ./test
1
2
5
6
Segmentation fault

I get no errors or warnings from gcc. And yes, I understand logical errors will not show up. Tried to run this through strace, but can't find where its failing or why.

Any advice much appreciated.

Last edited by Azrael; 4 Days Ago at 05:03 PM..
# 2  
Old 4 Days Ago
Use gdb. We could probably point you to your problem but you need to learn how to use gdb.
the commands you need are:
Quote:
break main <- stop right when the program starts.
step <- enter this repeatedly until you get a segfault.
print [varname] to view contents of a variable
help <- gives you more things to use to navigate
help [command] gives specific help.
compile & debug with
Code:
gcc filename.c -Wall -g -o filename
gdb filename
gdb>  break main
r 
[boom! it segfaults gdb will give you the exact line in the file]

Have fun.
The Following User Says Thank You to jim mcnamara For This Useful Post:
Azrael (3 Days Ago)
# 3  
Old 3 Days Ago
You have to follow the method I showed to-the-letter. (*arr[a][b])[c] is not the same as (*arr)[a][b][c].

Code:
void arrbyptr(int (*ptr[2][2])[2]) {
        int result, i, j, k;
    for ( i = 0; i < 2; ++i ){
        for ( j = 0; j < 2; ++j ){
            for ( k = 0; k < 2; ++k ){
                result = (*ptr)[i][j][k];
                printf("%d\n", result);
            }
        }
    }
}

The Following 2 Users Say Thank You to Corona688 For This Useful Post:
Azrael (3 Days Ago), jim mcnamara (2 Days Ago)
# 4  
Old 3 Days Ago
Ah... That did the trick! Sorry, I guess I was confused as main called the function with (*ptr[2][2])[2] and the function's parameter was in the same format. Usually I try to brute-force every possible combination before posting anything, but I guess I missed that.


I haven't used gdb in a while, but I will have to start again. I'm just more used to strace from work.



Fortunately this should do me on calling all the arrays in this project. Next time I should be posting about a different topic. Much thanks again for all of your patience!
👤 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
Calling an array of arrays in C. Azrael Programming 6 1 Week Ago 10:37 AM
Making an array of 2D arrays in C Azrael Programming 2 08-17-2018 05:32 AM
Looping through multiple arrays in C. Azrael Programming 4 08-08-2018 02:26 AM
Bash arrays: rebin/interpolate smaller array to large array f77hack Shell Programming and Scripting 2 02-13-2016 04:34 PM
Looping through the contents of array for output name ida1215 UNIX for Dummies Questions & Answers 1 06-03-2014 03:02 AM
Looping through arrays newbie2010 Shell Programming and Scripting 4 04-27-2013 03:12 PM
using arrays and also help with array.contains functionality vivek d r Shell Programming and Scripting 2 12-26-2011 07:57 AM
Help in Array looping and creating multiple lines sexyTrojan UNIX for Dummies Questions & Answers 0 11-02-2009 07:30 PM
KSH script not looping through array kelldan Shell Programming and Scripting 2 07-18-2008 11:30 AM
Perl question - looping through an array of hashrefs kregh99 Shell Programming and Scripting 2 03-19-2004 09:48 AM


All times are GMT -4. The time now is 11:53 AM.

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