I am new to C and am having a problem reading this matrix declaration in a function.
void foo(int (*array)[][3], int row, int col)
and in the function how do I access an array element - say to print it's value.
int (*array)[][3]
declares array to be a pointer to a 2D array whose second dimension is 3. Example usage:
#include <stdio.h>
void foo(int (*array)[][3], int row, int col)
{
printf("%d\n", (*array)[row][col]);
}
int main()
{
int array[10][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
foo(&array, 2, 2);
return 0;
}
Output:
9
(*array)[][3] is a pointer to 2D array.it can point to an int array of variable rows and 3 columns.Here is an example:
int main(void)
{
int arr[3][3] =
{
{0,0,0},
{1,0,0},
{1,1,0},
};
int (*array)[3][3],row,col;
array = &arr;
printf("arr[1][0] : %d\n",(*array)[1][0]);
}
Related
why I get segment error when I print 2-D array which is delivery by the "main" function?There is a condition that the array must be delivery as int** 。how can I do that?
#include <stdio.h>
void display_matrix(int** matrix, int row, int column) {
printf("%d\n", matrix[1][2]);
}
void main() {
int m[3][4] = {
{1, 3, 5, 7},
{10, 11, 16, 20},
{23, 30, 34, 60}
};
display_matrix((int**) m, 3, 4);
}
For starters according to the C Standard the function main without parameters shall be declared like
int main( void )
An array declared like
int m[3][4] = {
{1, 3, 5, 7},
{10, 11, 16, 20},
{23, 30, 34, 60}
};
used in expressions with rare exceptions is converted to a pointer to its first element of the type int ( * )[4].
So the types int ( * )[4] and the type int ** are not compatible. Dereferencing the pointer of the type int ** for the declared array above you will get the value 1 that is not a valid address. So the function invokes undefined behavior.
Thus a function that accepts such an array as an argument should be declared like
void display_matrix( int ( *matrix )[4], size_t rows );
and the function can be called like
display_matrix( m, 3 );
Or if your compiler supports variable length arrays then the function can be declared like
void display_matrix( size_t rows, size_t cols, int ( *matrix )[cols] );
and the function can be called like
display_matrix( 3, 4, m );
Such a declaration of a function
void display_matrix(int** matrix, int row, int column);
usually is used when you allocated arrays dynamically like
int **m = malloc( 3 * sizeof( int * ) );
for ( size_t i = 0; i < 3; i++ )
{
m[i] = malloc( 4 * sizeof( int ) );
}
//... initialization of the arrays and
display_matrix( m, 3, 4 );
So either define the array dynamically or use one more array declared like
int * a[3] = { m[0], m[1], m[2] };
and pass this array instead of the array m to your function.
In this case the array a used as an argument expression in this call
display_matrix( a, 3, 4 );
will be implicitly converted to a pointer of the type int **.
You are getting Segmentation Fault because you are passing an array of integers to where an array of pointers is expected.
You have to prepare an array of int*.
#include <stdio.h>
void display_matrix(int** matrix, int row, int column) {
printf("%d\n", matrix[1][2]);
}
int main(void) {
int m[3][4] = {
{1, 3, 5, 7},
{10, 11, 16, 20},
{23, 30, 34, 60}
};
int* m_ptr[3] = {m[0], m[1], m[2]};
display_matrix(m_ptr, 3, 4);
}
When I run this program-
#include <stdio.h>
void inc( int num[], int n)
{
int i;
n++;
for(i=0;i<10;i++)
num[i]++;
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int a=2;
inc (arr, a);
int i;
for(i=0;i<10;i++)
printf("%d ", arr[i]);
printf("\n%d ", a);
return 0;
}
I get the output-
2 3 4 5 6 7 8 9 10 1
2
I understand why the int is unchanged but I don't understand why the array is getting changed since I have not used pointers to call the array. I know that the function will make a different copy of n and assign n=a and all changes will happen to n only and a will be unchanged. Why the array is getting changed?
In C, except for a few cases, an array name decays (=gets implicitly converted) to a pointer to its first element.
This
void inc(int num[], int n)
is exactly same as this:
void inc(int *num, int n)
I have an array int arr[5] = {10, 2, 3, 5, 1}, and I want to pass in the last 4 elements (basically from index 1 to index4) into an argument as an array (so: [2, 3, 5, 1]). Is there a way to do this very simply (like how in Ruby you would be able to do arr[1..4]), or would I have to use a for loop?
You can manually increment the pointer by 1:
your_function(arr + 1)
Pointer arithmetic in C implicitly accounts for the size of the elements, so adding 1 will actually add 1 * sizeof(int)
For a closer analogue to array slicing from other languages, try this function:
int *slice_array(int *array, int start, int end) {
int numElements = (end - start + 1)
int numBytes = sizeof(int) * numElements;
int *slice = malloc(numBytes);
memcpy(slice, array + start, numBytes)
return slice;
}
It makes a slice of the array between the given start and end indices. Remember to free() the slice once you're done with it!
Answer
Given you current code:
int arr[5] = {10, 2, 3, 5, 1};
You can duplicate the range 1..4 by:
int arr_dup[4];
memcpy(arr_dup,arr+1,sizeof(int)*4);
Remember that your function definition should be a pointer, example:
void a_function(int *arr_arg); //Call via a_function(arr_dup);
Explanation
Arrays in c implemented as pointers (aka variables that hold memory addresses).
If you do arithmetic on the pointer, it will advance to the respective element. Example:
ptr + 1 //Next Element
ptr - 1 //Previous Element
#include <stdio.h>
#include <stdlib.h>
void my_function(int arr_size, int *arr)
{
int i;
for(i=0; i < arr_size; i++)
{
printf("[%d]:%d\n", i, arr[i]);
}
}
int main(int argc, char **argv)
{
int arr[] = { 10, 2, 3, 5, 1 };
(void)my_function(4, &arr[1]); /* TODO > use more flexible indexing */
return EXIT_SUCCESS;
}
I think you can use memcpy,memcpy can copy data byte to byte.In memmory,our data is binary,so even int is 4 bytes,we can copy it byte to byte.
int dst[4];
memcpy(dst,&arr[1],size(int) * 4);
I'm trying to sort an array A whose elements are indexes. The indexes refer to another array B whose value will determine the order of A. So, I would like to sort A such that B[ A[i] ] is increasing.
For example:
A = [0, 1, 4, 5, 7]
B = [5, 3, 8, 2, 2, 7, 1, 6, 3, 9]
Sorted A would be
A' = [ 7, 4, 1, 0, 5 ]
Is this possible with C's built-in sort, or am I going to have to write my own implementation?
EDIT: These arrays are local function variables.
If you want to use qsort, the best thing to-do would be to re-wrap the indexes in A and the values in B into a struct, and then make a comparator based on a new array that struct. For instance:
typedef struct
{
int index_from_A;
int value_from_B;
} index_value_wrapper;
index_value_wrapper index_wrapper_array[5];
for (int i=0; i < 5; i++)
{
index_wrapper_array[i].index_from_A = A[i];
index_wrapper_array[i].value_from_B = B[A[i]];
}
int comparitor (const void* lhs, const void* rhs)
{
return (lhs.value_from_B - rhs.value_from_B);
}
Now you can run qsort on the struct array and from there you can extract the proper sorted sequence you desired for the original array A without having to use a custom sorting function.
If you have it available, qsort_r provides a way to do this. You can give it context information in an additional parameter. That context is passed to the comparison function. You can access that additional information to extract the desired sorting information.
The Microsoft compiler has a similar one: qsort_s
I think you can use qsort and a custom comparator
int comparator(const void *x, const void *y)
{
return ( b[*(int*)x] - b[*(int*)y] );
}
Create another array C of type struct { int a_value; int b_value}, initialise each element to the values of each index of a and the value looked up from b. Sort that, traverse the sorted C copying the a_values back into A.
Viola. No, that's a large violin. Voila!
Use your rule as the comparison function to qsort (as long as B is longer than A):
#include <stdio.h>
#include <stdlib.h>
int A[] = {0, 1, 4, 5, 7};
int B[]= {5, 3, 8, 2, 2, 7, 1, 6, 3, 9};
int my_cmp(const void *a_, const void *b_,void *arg_)
{
const int *a = a_, *b = b_;
if(B[*a] == B[*b])
return 0;
else if (B[*a] < B[*b])
return -1;
else
return 1;
}
int main(int argc,char *arga[])
{
int i;
qsort(A,sizeof A/sizeof A[0] ,sizeof A[0],my_cmp);
puts("Sorted A");
for(i = 0 ; i < sizeof A/sizeof A[0]; i++) {
printf("A[%d] : %d B[A[%d]] : %d\n",i,A[i],i,B[A[i]]);
}
return 0;
}
This gives:
$ ./a.out
Sorted A
A[0] : 4 B[A[0]] : 2
A[1] : 1 B[A[1]] : 3
A[2] : 0 B[A[2]] : 5
A[3] : 7 B[A[3]] : 6
A[4] : 5 B[A[4]] : 7
Available on many platforms is also qsort_r(on linux you'll have to #define _GNU_SOURCE before including <stdlib.h> to use it. Using that, you'd change the comparison function to e.g.
int my_cmp(const void *a_, const void *b_,void *arg_)
{
const int *a = a_, *b = b_, *arg = arg_;
if(arg[*a] == arg[*b])
return 0;
else if (arg[*a] < arg[*b])
return -1;
else
return 1;
}
And call qsort_r like
qsort_r(A,sizeof A/sizeof A[0] ,sizeof A[0],my_cmp,B);
I'm trying to print all of the values in the four arrays by sending them to a separate function. But, the problem is I can't get the function to print the all of the integers in the array because I'm not sure what I could set the condition statement in the for loop to, that would be universal to any array of any size.
Right now the function only prints the first 11 numbers. I assume that's because the first number in that array is 11.
#include <stdio.h>
void print_array(int a[]);
void find_max(int b[]);
void find_min(int c[]);
void search(int d[]);
void SORT(int e[]);
int main(void)
{
int first[11] = {7,7,7,7,7,7,7,7,7,7,7};
int second[14] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2};
int third[16] = {-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int fourth[23] = {-3, 4, 33, 22, 9, -100, 2, 56, 57, 55, 2, 90, 2234, 32, 8, 123, 2, 33, 22, 22, 33, -1, -3};
print_array(&second[0]);
return(0);
}
void print_array(int a[])
{
int i;
for(i=0;i<*a;i++)
{
printf("%d ",a[i]);
}
}
Pass a second argument to your function that takes the length of the array. For example:
print_array(int *array, int length)
{
for (int i = 0; i < length; i++) { /* whatever */ }
}
The function has no way of knowing when the array ends. This piece of data simply does not exist unless you pass it manually. The array is just a sequence of bytes in the memory, it has no end delimiter. So you should add a parameter to the function telling it the length of the array.
Yep, this is how it works in C.
Change the function to:
void print_array(int a[], size_t a_size) {
int i;
for(i=0; i< a_size;i++)
// ...
And change the calling of the function to pass in the size:
print_array(second, sizeof(second)/sizeof(second[0]));
Which will calculate the memory size of the array (for a 4 int array on a 32 bit system it'll be 16) and divide it by the size of an int (on a 32 bit system, it's 4 bytes).
in C you can make it with a function and macro:
void printArray_(int *a, int len) {
for (int i = 0; i < len; i++) printf("%d ", a[i]);
}
#define printArray(arr) printArray_((arr), sizeof(arr)/sizeof(arr[0]))
int main(int argc, _TCHAR* argv[])
{
int data[] = { 1,2,3,4 };
printArray(data);
return 0;
}
output:
1 2 3 4
Change this line
print_array(&second[0]);
To
print_array(&second);
Because,
&second[0] just passes the reference to the element at 0th position,which will not be able to traverse the array.
And we cannot traverse the array passed by reference without the size.As there are arrays of varied size, we can compute the size of the array by,
int array_length = sizeof(array)/sizeof(array[0]);
Change the line
void print_array(int a[])
To
void print_array(int *a,int array_length)
And the function of array printing will be as,
void print_array(int *a,int array_length){
int i;
for(i=0;i<array_length;i++){
printf("%d ",*a);
a++; //for incrementing the position of array.
}
}