Sponsored Content
Top Forums Programming Making an array of 2D arrays in C Post 303021771 by Corona688 on Thursday 16th of August 2018 12:46:45 PM
Old 08-16-2018
int * isn't enough information for C to know what kind of array is being pointed to. That assumes a single integer or 1D array.

int *matrix[size][size] does not make pointers to 2d arrays -- it makes a 2d array of pointers.

Pointing to an array of known size takes this slightly wonky syntax:

int (*variablename[size][size]) [elements];

The stuff inside the red brackets defines the array being pointed to, the [elements] outside it defines the actual number of elements.

An example:

Code:
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 };
}

I thank @yifangt for pointing out to me this was even possible. C has a few weird corners.

Last edited by Corona688; 08-16-2018 at 05:16 PM..
These 2 Users Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

split and making an array inside another array

I want to run an awk split on a value that has been pushed through an array and I was wondering what the syntax should be?? e.g. running time strings through an array and trying to examine just minutes: 12:25:30 10:15:13 08:55:23 awk ' NR==FNR{ ... (2 Replies)
Discussion started by: dcfargo
2 Replies

2. Shell Programming and Scripting

Perl array of arrays

Hi, I am trying to assign an array as a value to one of the array element, I mean I have an array @KS and array @kr. I want array @KS to hold @kr as an element. So I am doin this $KS=@kr; But the value stored is number of elements in the @kr array. Can... (2 Replies)
Discussion started by: eamani_sun
2 Replies

3. Shell Programming and Scripting

Searching array of arrays in perl

Suppose there are two arrays of arrays: @A = ( , , , ); @B = ( , , , , ); For each of $A, $A, $A..., I want to find the corresponding one in @B (match the letter, like $A eq $B), and print out both the second item, for example, $A and $B. How can I do this in perl? grep + map? Hope I... (1 Reply)
Discussion started by: zx1106
1 Replies

4. Shell Programming and Scripting

Making array of string with bash

in.txt libgstreamer gstreamer-0_10 gstreamer-0_10-plugins-good gstreamer-0_10-plugins-base Output should be: libgstreamer gstreamer-0_10 gstreamer-0_10-plugins-good gstreamer0_10-plugins-base Then: #!/bin/sh v=(libgstreamer gstreamer-0_10 gstreamer-0_10-plugins-good... (5 Replies)
Discussion started by: cola
5 Replies

5. Programming

Perl arrays and loop through array

Hi All I need to get <STDIN> from a user. the <STDIN> is a range of number delimited by "," (comma) and can be with range delimited by "-". Example: 1,2,3,4-9,12,15,34-36,70 Now I need to get this from the user and go on each number and "Do something"... but when trying to do this as above... (2 Replies)
Discussion started by: RedGrinGo
2 Replies

6. Shell Programming and Scripting

using arrays and also help with array.contains functionality

here is what i have... i=1 while read line do if grep -i-q "create procedure"<<<$line then startline="$line" endline="blahblah" Get procedure name into a variable named procName procName="procedure name is stored" do some... (2 Replies)
Discussion started by: vivek d r
2 Replies

7. Shell Programming and Scripting

perl script :making array

Hi , i have a perl script which takes 'csv; file as input parameter and performs some tasks. CSV file format : MAGENTF,AGENTF8,mahfr001,tksfr01,. ./.profile 1>/dev/null 2>&1;ps -fe|grep 'gn1avm_agent -n AGENTF8'|grep gn1avm_agent|tr -s '' ''|cut -d ' ' -f 3|xargs kill -9,,. ./.profile... (3 Replies)
Discussion started by: deepakiniimt
3 Replies

8. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

9. Programming

Calling an array of arrays in C.

Corona688 was great in helping me learn how to create arrays that hold other two dimensional array here. Unfortunately I didn't think ask about how to implement or call them. Basically, I'm trying to call an array of two-dimensional arrays like this: declaration: int (*side_one) = { { white_l1,... (6 Replies)
Discussion started by: Azrael
6 Replies

10. Programming

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: #include <stdio.h> void... (3 Replies)
Discussion started by: Azrael
3 Replies
The talloc array functions(3)					      talloc					     The talloc array functions(3)

NAME
The talloc array functions - Talloc contains some handy helpers for handling Arrays conveniently. Modules The talloc string functions. talloc string allocation and manipulation functions. Functions void * talloc_array (const void *ctx,#type, unsigned count) Allocate an array. void * talloc_array_size (const void *ctx, size_t size, unsigned count) Allocate an array. void * talloc_array_ptrtype (const void *ctx, const void *ptr, unsigned count) Allocate an array into a typed pointer. size_t talloc_array_length (const void *ctx) Get the number of elements in a talloc'ed array. void * talloc_zero_array (const void *ctx,#type, unsigned count) Allocate a zero-initialized array. void * talloc_realloc (const void *ctx, void *ptr,#type, size_t count) Change the size of a talloc array. void * talloc_realloc_size (const void *ctx, void *ptr, size_t size) Untyped realloc to change the size of a talloc array. void * talloc_realloc_fn (const void *context, void *ptr, size_t size) Provide a function version of talloc_realloc_size. Detailed Description Talloc contains some handy helpers for handling Arrays conveniently. Function Documentation void* talloc_array (const void *ctx, #type, unsignedcount) Allocate an array. The macro is equivalent to: * (type *)talloc_size(ctx, sizeof(type) * count); * except that it provides integer overflow protection for the multiply, returning NULL if the multiply overflows. Parameters: ctx The talloc context to hang the result off. type The type that we want to allocate. count The number of 'type' elements you want to allocate. Returns: The allocated result, properly cast to 'type *', NULL on error. Example: * unsigned int *a, *b; * a = talloc_zero(NULL, unsigned int); * b = talloc_array(a, unsigned int, 100); * See Also: talloc() talloc_zero_array() size_t talloc_array_length (const void *ctx) Get the number of elements in a talloc'ed array. A talloc chunk carries its own size, so for talloc'ed arrays it is not necessary to store the number of elements explicitly. Parameters: ctx The allocated array. Returns: The number of elements in ctx. void* talloc_array_ptrtype (const void *ctx, const void *ptr, unsignedcount) Allocate an array into a typed pointer. The macro should be used when you have a pointer to an array and want to allocate memory of an array to point at with this pointer. When compiling with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size() and talloc_get_name() will return the current location in the source file and not the type. Parameters: ctx The talloc context to hang the result off. ptr The pointer you want to assign the result to. count The number of elements you want to allocate. Returns: The allocated memory chunk, properly casted. NULL on error. void* talloc_array_size (const void *ctx, size_tsize, unsignedcount) Allocate an array. Parameters: ctx The talloc context to hang the result off. size The size of an array element. count The number of elements you want to allocate. Returns: The allocated result, NULL on error. void* talloc_realloc (const void *ctx, void *ptr, #type, size_tcount) Change the size of a talloc array. The macro changes the size of a talloc pointer. The 'count' argument is the number of elements of type 'type' that you want the resulting pointer to hold. talloc_realloc() has the following equivalences: * talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type); * talloc_realloc(ctx, NULL, type, N) ==> talloc_array(ctx, type, N); * talloc_realloc(ctx, ptr, type, 0) ==> talloc_free(ptr); * The 'context' argument is only used if 'ptr' is NULL, otherwise it is ignored. Parameters: ctx The parent context used if ptr is NULL. ptr The chunk to be resized. type The type of the array element inside ptr. count The intended number of array elements. Returns: The new array, NULL on error. The call will fail either due to a lack of memory, or because the pointer has more than one parent (see talloc_reference()). void* talloc_realloc_fn (const void *context, void *ptr, size_tsize) Provide a function version of talloc_realloc_size. This is a non-macro version of talloc_realloc(), which is useful as libraries sometimes want a ralloc function pointer. A realloc() implementation encapsulates the functionality of malloc(), free() and realloc() in one call, which is why it is useful to be able to pass around a single function pointer. Parameters: context The parent context used if ptr is NULL. ptr The chunk to be resized. size The new chunk size. Returns: The new chunk, NULL on error. void* talloc_realloc_size (const void *ctx, void *ptr, size_tsize) Untyped realloc to change the size of a talloc array. The macro is useful when the type is not known so the typesafe talloc_realloc() cannot be used. Parameters: ctx The parent context used if 'ptr' is NULL. ptr The chunk to be resized. size The new chunk size. Returns: The new array, NULL on error. void* talloc_zero_array (const void *ctx, #type, unsignedcount) Allocate a zero-initialized array. Parameters: ctx The talloc context to hang the result off. type The type that we want to allocate. count The number of 'type' elements you want to allocate. Returns: The allocated result casted to 'type *', NULL on error. The talloc_zero_array() macro is equivalent to: * ptr = talloc_array(ctx, type, count); * if (ptr) memset(ptr, sizeof(type) * count); * Author Generated automatically by Doxygen for talloc from the source code. Version 2.0 Tue Jun 17 2014 The talloc array functions(3)
All times are GMT -4. The time now is 10:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy