How to iterate through and copy double pointer in C - c

I've created a two dimensional integer array in C, initialised it with values, and then cast it to an int** (I have to do that because it's for homework).
I've managed to iterate through it and set all of the values to 0. However, when I iterate through it again and print the values it, the output is not all zeros.
Here is a minimum working example:
#include <string.h>
#include <stdio.h>
#define ROWS 3
#define COLS 2
int main(void)
{
/* Create array and convert to double pointer */
int c[ROWS][COLS] = {{1,2},{3,5},{8,13}};
int **ptr = (int **)c;
/* Loop through entire array and print then double each value. */
int *temp[ROWS];
for(int i = 0; i<ROWS; i++){
temp[i] = (int*)(ptr+i);
for(int j = 0; j<COLS; j++){
printf("Before setting: %i\n", temp[i][j]);
temp[i][j] = temp[i][j]*2;
}
}
/* Copy temp back into ptr */
memcpy(ptr, temp, sizeof(ptr));
/* Loop through array and print values */
int *temp2[ROWS];
for(int i = 0; i<ROWS; i++){
temp2[i] = (int*)(ptr+i);
for(int j = 0; j<COLS; j++){
printf("After setting: %i\n", temp2[i][j]);
}
}
}
The issue is that the results are not what I would expect. One time I ran it, this was the output:
Before setting: 1
Before setting: 2
Before setting: 3
Before setting: 5
Before setting: 8
Before setting: 13
After setting: -1193330832
After setting: 32764
After setting: 6
After setting: 10
After setting: 16
After setting: 26
The value 32764 is the same every time the program is run, but the value -1193330832 changes each time (I assume it is the memory address of the array).
The output I was expecting is:
Before setting: 1
Before setting: 2
Before setting: 3
Before setting: 5
Before setting: 8
Before setting: 13
After setting: 1
After setting: 4
After setting: 6
After setting: 10
After setting: 16
After setting: 26
because the values in the first loop have been doubled.
What have I done wrong? Why are the values changing and how should I actually go about fixing this?
(P.S. the homework doesn't involve finding a way to iterate through the double pointer, but I need to be able to do it to complete the actual task)

int **ptr = (int **)c; isn't a valid pointer conversion, since you cannot use a pointer-to-pointer to point at a 2D array. Because it has nothing to do with 2D arrays.
Instead you can use a pointer to a 2D array, int (*)[ROWS][COLS];. The most convenient is however to use a pointer to a 1D array and have it point at the first element of the 2D array:
int (*ptr)[COLS] = &c[0];
...
ptr[i][j] = ...;
Fixed example:
#include <string.h>
#include <stdio.h>
#define ROWS 3
#define COLS 2
int main(void)
{
/* Create array and convert to double pointer */
int c[ROWS][COLS] = {{1,2},{3,5},{8,13}};
int (*ptr)[COLS] = &c[0];
/* Loop through entire array and print then double each value. */
for(int i = 0; i<ROWS; i++){
for(int j = 0; j<COLS; j++){
printf("Before setting: %i\n", ptr[i][j]);
ptr[i][j] = ptr[i][j]*2;
}
}
/* Loop through array and print values */
for(int i = 0; i<ROWS; i++){
for(int j = 0; j<COLS; j++){
printf("After setting: %i\n", ptr[i][j]);
}
}
}
(A matter of style, but your order of ROWS and COLS is a bit weird, it is more common to do int[COLS][ROWS] then for(i=0; i<COLS; i++))

Related

Variable sized array in C

For some reason, the first output my program is giving, is garbage value, while the second output is correct.
This is a problem from HackerRank.
I know this question has already been asked by someone else. I just want to know what the problem is in my code.
#include <stdio.h>
int main()
{
int index,query;
int count1 = 0;
scanf("%d%d",&index,&query);
for(int i=0;i<index;i++)
{
int b;
scanf("%d",&b);
int A[index][b];
for(int j=0;j<b;j++)
{
scanf("%d",&A[i][j]);
}
count1++;
if(count1<index)
{
continue;
}
int count2=0;
while(count2<query)
{
int d,e;
scanf("%d%d",&d,&e);
printf("%d\n",A[d][e]);
count2++;
}
}
return 0;
}
If the input is:
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3
Then the output should be:
5
9
But instead, my output is:
garbage
9
Disclaimer
I didn't even click the link, so I do not know if the solution is correct, but assuming that you got the logic right..
The problem
is that you populate in stages a local to the body of the for loop 2D array, which at the end of your processing, you expect to have it accessible (I mean the complete matrix, populated from every single iteration of the for loop).
Instead, you get only the last's iteration declared array, that's why you get only the A[1][3] element right, and not the A[0][1], since the 2nd row is populated in the last (2nd iteration), while the 1st row is populated in the first iteration (of the firstly declared A), which goes out of scope as soon as the first iteration terminates.
The fix
So, what you need to fix this is to dynamically allocate memory for your matrix, and every time a new dimension for the columns is inputed, resize it with realloc().
I believe that the explanation I have in 2D dynamic array (C) will help you, since what you want is the number of rows fixed, and the number of columns adjustable on every iteration.
Below is an illustration based on the link I shared above, which visualizes what exactly is your matrix (a 1D array of pointers), and shows how the code below manipulates it:
Full code example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int index,query;
int count1 = 0;
scanf("%d%d",&index,&query);
// An array of `index` (e.g. 2) pointers to integers
int *A[index];
// Initialize all the pointers to NULL
for(int k = 0; k < index; ++k)
A[k] = NULL;
for(int i=0;i<index;i++)
{
int b;
scanf("%d",&b);
// Replaced your 'int A[index][b];' with the following:
// Every time a new number of columns (that's 'b') is read,
// we need to adjust the numbers of columns our matrix ('A') has.
// That means, that for every pointer (row), we need to re-allocate
// the number of columns it points to, which is basically a 1D array, of dimension 'b'
for(int k = 0; k < index; ++k)
A[k] = realloc(A[k], b * sizeof(int) );
for(int j=0;j<b;j++)
{
scanf("%d",&A[i][j]);
}
count1 ++;
if(count1<index)
{
continue;
}
int count2=0;
while(count2<query)
{
int d,e;
scanf("%d%d",&d,&e);
printf("%d\n",A[d][e]);
count2++;
}
}
// Free the dynamically allocated memory
for(int k = 0; k < index; ++k)
free(A[k]);
return 0;
}
Output (for the input provided):
5
9
Pro-tip: The typical methodology of calling realloc() is to use a specific pointer for the reallocation, test that pointer and, if everything worked out ok, change the old pointer, as explained in Does realloc overwrite old contents?, which I didn't do in that post for the sake of being "laconic".
The C VLA is not suitable here. It seems you need to allocate memory dynamically. The only VLA that can be used is an array of pointers to other arrays. All other arrays should be allocated dynamically.
Something like the following.
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
size_t number_of_arrays;
size_t number_of_queries;
scanf( "%zu%zu", &number_of_arrays, &number_of_queries );
int **a = malloc( number_of_arrays * sizeof( int * ) );
for ( size_t i = 0; i < number_of_arrays; i++ )
{
size_t size;
scanf( "%zu", &size );
a[i] = malloc( size * sizeof( int ) );
for ( size_t j = 0; j < size; j++ ) scanf( "%d", &a[i][j] );
}
for ( size_t i = 0; i < number_of_queries; i++ )
{
size_t array_index;
size_t element_index;
scanf( "%zu%zu", &array_index, &element_index );
printf( "%d\n", a[array_index][element_index] );
}
for ( size_t i = 0; i < number_of_arrays; i++ ) free( a[i] );
free( a );
}
If to input
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3
then the program output will be
5
9
As for your code then it is invalid. For example the variable b is not initialized so the declaration of the array has undefined behavior.
int b;
scanf("%d",&b);
int A[index][b];
^^^
Hint : Variable sized arrays need to be dynamically allocated, here's how to it in C
int rows;
scanf("%d",&rows);
//2D array
int**A = malloc(sizeof(int*)*rows); //allocate number of rows
//for each row allocate number of colums
for(int i = 0; i < rows; i++)
{
int cols;
scanf("%d",&cols);
A[i] = malloc(sizeof(int)*cols);
}

Arduino C Multi dimensional array switching

Im stuck with a piece of my Arduino code
Basically I have a multi dimensional array in which every index will store a random letter. But after a small time (eg. 900 milliseconds) every value in the index will move up to the index above it. Ofcourse when the index reaches e.g. [9][0] it will go to the largest index like [9][9]
E.g.: myArray[9][8] contains "K" and myArray[9][7] contains "L"
The "K" in myArray[9][8] will be put in myArray[9][7] and the "L" in myArray[9][7] will go to myArray[9][6] etc. etc
But I have no clue how to do this in C...
myArray[9][8] = myArray[9][7] obviously wouldnt work
There is not problem with assignment, but you must save the element you are writing over first.
In this example all the rewritten values are kept in the last slot of every row, this is just an example- not the only way to go, if you need to move the letters across the rows as well the idea is similar.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void moveLettersInRow(char sArr[][3], int row)
{
int i = 0;
int j = 0;
char temp ;
for (i = 0; i < row; ++i)
{
for(j = 2 ;j > 0;--j)
{
temp = sArr[i][j-1]; /*save the value to be rewritten */
sArr[i][j-1]= sArr[i][2]; /* set the last val in its new place*/
sArr[i][2]= temp; /* set the last elem to the latest rewritten value*/
}
}
}
void printArr(char arr[][3], int size)
{
int i = 0, j= 0;
for(i = 0; i< size; ++i)
{
for(j = 0; j< 3; ++j)
{
printf("%c | ", arr[i][j]);
}
printf("\n");
}
}
int main()
{
char arr[2][3]= {{'a','b','c'},{'a','b','c'}};
moveLettersInRow(arr, 2);
printArr(arr, 2);
return 0;
}
There are several ways to go about this. One would be to use a temporary variable while copying, as Kami Cuk and Fred pointed out.
Another way, which would avoid copying the values, would be to index the array using a modulo operation while reading the array.
myArray[9][(index + offset) % numberOfElementsInArray]
index and offset should both be positive integers.
numberOfElementsInArray is the number of elements in myArray[9]
To get a -1 offset with a positive integer, set offset to numberOfElementsInArray - 1

Reordering the rows in a matrix in a specific order

I am successfully storing the calculated subsets in a 2-D array matrix in C language.Now I want to print the subsets in an order desired.
For eg.
2-D array matrix is
10 7 3 2 1
10 7 5 1
7 6 5 3 2
10 6 5 2
10 7 6
Desired Output
10 7 6
10 7 5 1
10 7 3 2 1
10 6 5 2
7 6 5 3 2
How quick sort can be applied to sort/order these rows?
As #chqrlie noted, this can be easily solved with qsort.
Depending on the way the matrix is declared (is it an array of pointers to arrays of ints? do all arrays have the same length? is it a global array of fixed size?) the code will have to do slightly different things.
So, assuming the array is a global variable and all rows have same length (padded with 0s):
MWE:
#include <stdio.h>
#include <stdlib.h>
/*
Compare 2 integers
returns:
-1 if *i1 < *i2
+1 if *i1 > *i2
0 if *i1 == *i2
*/
int intcmp(const int *i1, const int *i2)
{
return (*i2 < *i1) - (*i1 < *i2);
}
#define ROWS 5
#define COLS 5
/*
Assumes rows already sorted in descending order
NOTE: qsort calls the comparison function with pointers to elements
so this function has to be tweaked in case the matrix is an array of
pointers. In that case the function's declaration would be:
int rowcmp(int **pr1, int **pr2)
{
const int *r1 = *pr1;
const int *r2 = *pr2;
// the rest is the same
}
*/
int rowcmp(const int *r1, const int *r2)
{
int i = 0, cmp;
do {
cmp = intcmp(&r1[i], &r2[i]);
i++;
} while (i < COLS && cmp == 0);
return -cmp; /* return -cmp to sort in descending order */
}
int data[5][5] = {
{10,7,3,2,1},
{10,7,5,1,0},
{ 7,6,5,3,2},
{10,6,5,2,0},
{10,7,6,0,0}
};
void printmatrix()
{
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("%d ", data[i][j]); /* leaves a trailing space in each row */
}
printf("\n");
}
}
int main()
{
printmatrix();
qsort(data, 5, sizeof(data[0]), (int (*)(const void *, const void *))rowcmp);
printf("\n");
printmatrix();
return 0;
}
For the most flexible solution, I would define
struct row {
size_t len;
int *elems;
};
struct matrix {
struct row *rows;
size_t nrows;
};
and change the code accordingly.
NOTE: code not thoroughly tested, use with caution ;)
First of all, are you sure that the 1 on row 3,col 5 should be there and not on the last line?
Anyway, an efficient way to achieve what you want is:
compute the frequency array
declare a new matrix
go from the highest element (10 in your case) from frequency array and put in your matrix using your desired format.
It is time-efficient because you don't use any sorting algorithm, thus you don't waste time there.
It is NOT space-efficient because you use 2 matrices and 1 array, instead of only 1 matrix as suggested in other posts, but this should not be a problem, unless you use matrices of millions of rows and columns
C code for frequency array:
int freq[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for(int i=0; i<NO_ROWS; i++) {
for(int j=0; j<NO_COLS; j++) {
if(MATRIX[i][j]!=null && MATRIX[i][j]>0 && MATRIX[i][j]<11) {
freq[MATRIX[i][j]]++;
}
}
}
C code for computing the new matrix dimensions
(assuming you want to keep the number of rows)
OUTPUT_MATRIX[100][100] /*I declared it statically, but I would advise to make it dinamically */
/* first, compute the number columns.
To do so, we need the number of elements
(we get them by simply summing up frequency array's elements) */
int s=0;
for(int i=0; i<11; i++) {
s+=frequency[i];
}
int addOne = 0 /* boolean value to check if we will have to add one extra column for safety */
if(s % NO_ROWS) {
addOne = 1; /* division is not even, so we will have to add extra column */
}
NO_COLS = s/NO_ROWS + addOne;
Now, final part, assigning the values from frequency array to the OUTPUT_MATRIX
int k=0;
int currentNumber = 10; /* assigning starts from 10 */
for(int i=0; i<NO_ROWS; i++) {
for(int j=0; j<NO_COLS; j++) {
if(currentNumber>0) {
if(frequency[currentNumber]==0 || k>=frequency[currentNumber]) {
currentNumber--;
k=0;
}
OUTPUT_MATRIX[i][j] = frequency[currentNumber];
k++;
} else {/*here, you can assign the rest of the value with whatever you want
I will just put 0's */
OUTPUTMATRIX[i][j] = 0;
}
}
}
Hope this helps!
This is what I do in C++ to reorder a matrix:
// b is the matrix and p is an array of integer containing the desired order of rows
for(i=0; i<n; i++){
if( p[i]==i )
continue;
b[i].swap(b[p[i]]);
j = p[i]; // New row i position
// Update row i position to new one
for(int k=i+1; k<n; k++){
if( p[k] == i )
p[k] = j;
}
printRow( b[i] );
}
You need to define an array of pointers of the data type you use and then you can reorder your matrix.
for example your matrix is: arr[5][10], and you want to print line 4 before line 3:
int *[5] arr2;
arr2[0] = &arr[0][0];
arr2[1] = &arr[1][0];
arr2[2] = &arr[2][0];
arr2[3] = &arr[4][0];
arr2[4] = &arr[3][0];
in regard to how will the ordering algorithm work, i would suggest placing a header in the start of each array in the matrix which will tell you how many elements it has(basically the first element of each array can be a counter of the total elements) afterwards you can order the strings by comparing the header, and if it is equal comparing the first element and so on. this can be done in a loop that iterates as many times as there are elements in the array, when the elements are not equal, break out of the loop.
hope this helps.

printing two dimensional array in c

I am trying to define a two dimensional array by initially defining elements for 5 x 2 matrix and then I am defining again the elements for 6th row. But when I try to print the elements of this matrix I am getting 0 and 5 for the last value. Tried same by defining elements again for 4th or 6th row but then it is working fine.
#include<math.h>
#include<stdio.h>
main()
{
int arr[ ][2]={{11,12},{21,22},{31,32},{41,42},{51,52}};
int i, j;
arr[5][0]=61; arr[5][1]=62;
for (i=0;i<=5;i++)
{
for (j=0;j<=1;j++)
{
printf ("%d \n", arr[i][j]);
}
}
}
Your initialised array is given exactly enough memory to hold the specified data values. So
int arr[ ][2]={{11,12},{21,22},{31,32},{41,42},{51,52}};
creates the array as int arr[5][2] and then the line
arr[5][0]=61; arr[5][1]=62;
exceeds the array bounds. The maximum index is [4][1] because array indexing is 0 based. If you want to add another element you should specify
int arr[6][2]={{11,12},{21,22},{31,32},{41,42},{51,52}};
and then this line will work.
arr[5][0]=61; arr[5][1]=62;
An alternative would be to use malloc() to allocate memory for the array, and then if you want to add another row you can use realloc(), and this shows how to make a flexible array.
#include <stdio.h>
#include <stdlib.h>
#define COLUMNS 2
#define ROWS 5
typedef int rowtype[COLUMNS];
int main() {
int i, j;
rowtype *array = malloc(ROWS * sizeof(rowtype));
if (array == NULL)
return -1;
for (j=0; j<ROWS; j++)
for (i=0; i<COLUMNS; i++)
array[j][i] = (j+1)*10 + (i+1);
for (j=0; j<ROWS; j++) {
for (i=0; i<COLUMNS; i++)
printf ("%-4d", array[j][i]);
printf ("\n");
}
printf("Add another row\n");
array = realloc(array, (ROWS+1) * sizeof(rowtype));
if (array == NULL)
return -1;
array[ROWS][0] = 61;
array[ROWS][1] = 62;
for (j=0; j<ROWS+1; j++) {
for (i=0; i<COLUMNS; i++)
printf ("%-4d", array[j][i]);
printf ("\n");
}
free(array);
return 0;
}
Program output:
11 12
21 22
31 32
41 42
51 52
Add another row
11 12
21 22
31 32
41 42
51 52
61 62

Reading an array of integers and printing them out

I'm learning C on my own and doing a few exercises. The following code reads in an
array of integers from the user. The integers are printed out when the user types in a "0" or when the array is filled. Now the problem is the output. When I type in "0" after I have typed in 3 digits e.g. 1 2 3 the output is the following: 1 2 3 -858993460 -858993460. I am not sure why I get the value "-858993460" but I have already found a solution to avoid it. Now my question is what the values mean and if there is a smarter solution than mine which is presented below as comments.
#include <stdio.h>
#include <string.h>
#define arraylength 5
int main ()
{
//const int arraylength = 21; //alternative possibility to declare a constant
int input [arraylength] ;
int temp = 0;
//int imax = 0;
printf("Please type in a your digits: ");
for (int i = 0; i < arraylength; i++)
{
scanf("%d", &temp);
if ( temp !=0)
{
input[i]= temp;
//imax= i;
}
else
{
//imax= i;
break;
}
if (i < arraylength-1)
printf("Next: ");
}
for (int i =0; i < arraylength; i++ ) // switch arraylength with imax
{
printf("%d", input[i]);
}
getchar();
getchar();
getchar();
}
This happens because irrespective of when the 0 input is given you print all 5 numbers:
for (int i =0; i < arraylength; i++ )
To fix this you can print only the number(s) user entered before entering a 0 by running a loop from 0 to i:
for (int j =0; j < i; j++ )
Those 2 numbers are the garbage that was left in the memory locations for the last 2 parts of your array. You never initialise those when you only input 3 numbers, so when you go through and print all 5 elements in the array, it prints whatever garbage was in the memory.
You print all integers in array which is size of arraylength = 5. So you get 5 integers in output. As you didn't initialize array, you get uninitilized values as 4th and 5th elements of array. You can use memset(&input, 0, arraylength*sizeof(int)); to set initials values in array to 0.

Resources