2D Int Array print using pointers - c

While executing the below code:
int main()
{
int abc[3][3]={0};
for(int *ip=&abc[0][0];ip<=&abc[3][3];ip++)
{
printf("%d \n",*ip);
}
}
Expected result is 9 zeros but it displays 12 data. What might be reason?

If you look at the memory layout of a 3x3 array, it looks like:
[0][0] [1][0] [2][0]
+----+----+----+----+----+----+----+----+----+
| | | | | | | | | |
+----+----+----+----+----+----+----+----+----+
Where is the element [3][3]?
[0][0] [1][0] [2][0] [3][0] [3][3]
+----+----+----+----+----+----+----+----+----+----+----+----+----+
| | | | | | | | | | | | | |
+----+----+----+----+----+----+----+----+----+----+----+----+----+
That explains why you end up accessing 12 elements.
Your code is subject to undefined behavior for accessing the beyond valid indices but that's another issue.
You could use:
for (int *ip = &abc[0][0]; ip <= &abc[2][2]; ip++)
{
printf("%d \n",*ip);
}
However, it is better to access a 2D array as a 2D array.
for (size_t i = 0; i < 3; ++i )
{
for (size_t j = 0; j < 3; ++j )
{
printf("%d \n", abc[i][j]);
}
}

Related

The pointer access representation of the program

The code ran differently than I predicted, I think *(score+i*n+j) is problematic, it could also be a problem elsewhere, I'm not quite sure, but I don't know how to modify it.
#include <stdio.h>
#define STUD 30 // Maximum number of students possible
#define COURSE 5 // The maximum number of possible exam subjects
void Total(int *score, int sum[], float aver[], int m, int n);
void Print(int *score, int sum[], float aver[], int m, int n);
int main(void)
{
int i, j, m, n, score[STUD][COURSE], sum[STUD];
float aver[STUD];
printf("Enter the total number of students and courses:\n");
scanf("%d %d",&m,&n);
printf("Enter score:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &score[i][j]);
}
}
Total(*score, sum, aver, m, n);
Print(*score, sum, aver, m, n);
return 0;
}
void Total(int *score, int sum[], float aver[], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
sum[i] = 0;
for (j = 0; j < n; j++)
{
sum[i] = sum[i] + *(score + i * n + j);
}
aver[i] = (float) sum[i] / n;
}
}
void Print(int *score, int sum[], float aver[], int m, int n)
{
int i, j;
printf("Result:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%4d\t", *(score + i * n + j));
}
printf("%5d\t%6.1f\n", sum[i], aver[i]);
}
}
Example of a program running:
Enter the total number of students and courses:
2 3↙
Enter score:
90↙
95↙
97↙
82↙
73↙
69↙
Result:
90 95 97 282 94.0
82 73 69 224 74.7
Compiling your program yields no warnings or errors. Running it with the sample input you've provided yields:
Enter the total number of students and courses:
2 3
Enter score:
90
95
97
82
73
69
Result:
90 95 97 282 94.0
404780 0 82 404862 134954.0
This is correct for the first set of scores, but not the second. As you intuited, this means your math for accessing the array via pointer math is probably wrong.
Consider what your array actually looks like in memory. You've allocated on the stack an array that looks like:
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
| | | | | |
+---+---+---+---+---+
Your example input has filled it like this:
+---+---+---+---+---+
| 90| 95| 97| | |
+---+---+---+---+---+
| 82| 73| 69| | |
+---+---+---+---+---+
...
If you want to access the first element of the second row, you need your offset to be i * 5 rather than i * 3 which is what happens when you use i * n. This 5 we can get from your constant COURSE.
*(score + i * COURSE + j)
When you use a different offset you get data which has not been initialized, which is why you see garbage values. If you initialize all of the values in your array to 0, but leave your code otherwise unchanged, you can see this in action.
int i, j, m, n, score[STUD][COURSE] = {0}, sum[STUD];
Enter the total number of students and courses:
2 3
Enter score:
90
95
97
82
73
69
Result:
90 95 97 282 94.0
0 0 82 82 27.3
As you note, the problem is your array accesses -- you use score[i][j] in main to fill the array and then *(score + i * n + j) in your Total and Print functions to try to access it, and these are different and incompatible. The easiest fix is probably just to fix the declarations of Total and Print to match the score you are using:
void Total(int score[][COURSE], int sum[], float aver[], int m, int n);
void Print(int score[][COURSE], int sum[], float aver[], int m, int n);
Then you can just use score[i][j] in them and everything should work. You would also pass score as just score instead of *score.
Alternately, change the declaration of score to score[STUD*COURSE] and use *(score + i * n + j) (or score[i*n + j]) in main to access it like you do in Total and Print.
OP is unclear why the code uses #defines to define values for rows and columns of the array score, then goes on to use scanf() to enter new values that may or may not conflict with the #defines, or even overflow the array memory. Either method works, but using both together confuse things. Pick one or the other.
Aside: If a dynamic sized array is necessary, then it can be created as a pointer, or pointers to allocated memory, or by use of a VLA
eg: A short example of using user input with dynamic memory allocation to create the array sized to the need of the moment:
Note: following method allows you to use plain array notation to assign values:
score[i][j] = someValue://easy to use and readable
//as opposed to
*(score + i*n + j) = someValue;// cumbersome to use and read
Example:
int student, course;//using descriptive variables
printf("Enter the total number of students and courses:\n");
scanf("%d %d",&student,&course);
int (*score)[course] = create_arr_2d (student, course);
if(score)
{ //use score
...
free(score);
}
Where create_arr_2d(...) is defined:
void * create_arr_2d (size_t x, size_t y)
{
int (*ptr_2)[x] = malloc( sizeof (int *[y]) ); //allocate a true 2D array
if(ptr_2)
{
memset(ptr_2, 0, sizeof **ptr_2);
}
return ptr_2;
}
(credit for method)
Addressing your code as is. First, the following creates variables, but does not initialize any of them:
int i, j, m, n, score[STUD][COURSE], sum[STUD];
float aver[STUD];
To eliminate some of the issues you may be seeing, initialize:
int i=0, j=0, m=0, n=0, score[STUD][COURSE]={{0}}, sum[STUD]={0};
float aver[STUD]={0};
In the function prototype in your given code:
Total(int *score, int sum[], float aver[], int m, int n)
int *score
suggests a pointer to a single dimensional array is being passed, but if it is being used to represent score[STUD][COURSE], then it should be passed as `score[m][n], with the prototype changed as:
Total(int m, int n, int score[m][n], int sum[m], float aver[m]);
Then called as:
Total(STUD, COURSE, score[STUD][COURSE], sum[STUD], aver[STUD]){...}
Note, this arrangement makes use of VLA type function arguemnts
Note also, an array such as: (shortened from your values for easier viewing)
int m = 5;
int n = 4
int array[m][n] = {{0}}
creates a single contiguous block of memory, conceivably looking like this in memory:
`|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|...`
0 5 10 15 20
Where all of the elements can be accessed in a for loop like this:
for(int i=0; i<m; i++
for(int j=0;j<n;j++)
*(array + i*n + j);
...
...

Trying to convert every string to an integer and storing each element into int**

I have an object of type int** which I want to fill with integers that were originally strings, in this case fill it with the integer 1234 that was originally a string "1234".
int** num_list = malloc(10*sizeof(int*));
for(int i=0; i<10; i++) {
num_list[i] = atoi("1234");
}
free(num_list);
The error / warning I get is assignment makes pointer to integer without a cast [-Wint -conversion]. I'm still new to C language and after failing with various attempts of making this work correctly I came to ask here, why is this happening and how can this work without issues? I cannot change the type of the list, it needs to stay as int** because all my work depends on this type and I cannot change it to something else.
You could do this:
int** num_list_1 = malloc(10*sizeof(int*));
for(int i=0; i<10; i++) {
num_list_1[i] = malloc(sizeof int);
num_list_1[i][0] = atoi("1234");
}
for(int i=0; i<10; i++) {
free(num_list_1[i]);
}
free(num_list_1);
Or you could do this:
int** num_list_2 = malloc(10*sizeof(int*));
num_list_2[0] = malloc(10*sizeof(int));
for(int i=1; i<10; i++) {
num_list_2[i] = NULL;
}
for(int i=0; i<10; i++) {
num_list_2[0][i] = atoi("1234");
}
Notice that in the first example the key assignment is
num_list_1[i][0] = atoi("1234");
while in the second example it is
num_list_2[0][i] = atoi("1234");
Another way to write the first (not the second) example would be
*num_list_1[i] = atoi("1234");
The first example looks like this in memory:
+---+
num_list_1: | * |
+-|-+
|
v
+---+ +------+
| *----> | 1234 |
+---+ +------+ +------+
| *-----------------> | 1234 |
+---+ +------+ +------+
| *----> | 1234 |
+---+ +------+
.
.
.
+---+ +------+
| *----> | 1234 |
+---+ +------+
The second example looks like this:
+---+
num_list_2: | * |
+-|-+
|
v
+---+ +------+------+------+ +------+
| *----> | 1234 | 1234 | 1234 | ... | 1234 |
+---+ +------+------+------+ +------+
| 0 |
+---+
| 0 |
+---+
.
.
.
+---+
| 0 |
+---+
Notice that in each case, num_list_1 and num_list_2 point to an array of pointers, and each of those second-level pointers actually points to an int (or to several ints in the case of num_list_2).
If you could use int * (as in SlLoWre's answer) it would look like this:
+---+ +------+------+------+ +------+
num_list: | *----> | 1234 | 1234 | 1234 | ... | 1234 |
+---+ +------+------+------+ +------+
But, again, if you want this picture, num_list has to be an int *, not an int **.
The requirement to use int ** seems strange. The only reason I can imagine that you would use an int ** would be if you are using a function to construct the array, and the function is returning the pointer by reference. That would look like this:
void allocate_array(int **num_list_p)
{
*num_list_p = malloc(10*sizeof(int*));
for(int i=0; i<10; i++) {
(*num_list_p)[i] = atoi("1234");
}
}
int main()
{
int *num_list;
allocate_array(&num_list);
for(int i = 0; i < 10; i++) {
printf("%d: %d\n", i, num_list[i]);
}
}
Also, one last thing: You probably knew this, but in real code, we would always check the return value of malloc to make sure it wasn't NULL.
You dont need to have int** (pointer to a pointer) use just int*
int* num_list = malloc(10*sizeof(int));
for(int i=0; i<10; i++) {
num_list[i] = atoi("1234");
}
free(num_list);

How to add items to array in a function using pointers?

I am trying to write this function that takes an array input and assigns random numbers between 1-52 to the array. Then prints back the numbers. I get an error message on the 4th line that says expression is not assignable. Is my use of pointers incorrect or is it not possible to add content to array with pointers?
void shuffleDeck(int *deck[]) {
int i;
for(i=0;i<=52;i++)
rand()%53 = deck[i]; // I get an error message on this line
}
int main() {
srand(time(NULL));
int deck[2000];
shuffleDeck(deck);
int i;
for(i=0;i<=52;i++){
printf("%d", deck[i]);
}
return 0;
}
Assigment direction is from right to left
So in this expression
deck[i] = rand()%52 + 1;
First is called function rand()
Then the result of function %52
Then 1 is added
And finnally its assigned to deck[i]
Array (what you have in main) is
0 1 2 3 4
+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+
^-------------------------- arr[0]
While Array of pointers (what your function expects) is
0 1 2 3 4
+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0
+---+---+---+---+---+
------^
| +---+---+---+---+---+
| | 0 | 0 | 0 | 0 | 0 | 1
| +---+---+---+---+---+
| +---+---+---+---+---+
| | 0 | 0 | 0 | 0 | 0 | 2
| +---+---+---+---+---+
|
----------------------------- arr[0][0]
When array is passed to function, then arr[] and *arr are the same. But only as function arguments!
%53 returns 0-52,so if you need 1-52, use %52 + 1 (0-51 + 1 = 1-52)
If you cant solve it, here is code with fixed bugs.
void shuffleDeck(int deck[]) {
int i;
for(i=0;i<=52;i++)
deck[i] = rand()%52 + 1;
}
int main() {
srand(time(NULL));
int deck[2000];
shuffleDeck(deck);
int i;
for(i=0;i<=52;i++){
printf("%d", deck[i]);
}
return 0;
}

Tic Tac Toe AI issue in C

I tried making a simple tic tac toe project, and I am trying to make the AI move to random places, so that the AI just moves into any available place. However, it always makes 2 moves. I am wondering why it always makes 2 moves instead of just 1.
What I mean is, let's say the user inputs 'O' into slot 1,1.
0 1 2
+----+----+----+
0 | | | |
| | | |
+----+----+----+
| | | |
1 | | O | |
+----+----+----+
| | | |
2 | | | |
+---------------
Then the computer moves 2 moves: into slots 1,2 AND 2,0
0 1 2
+----+----+----+
0 | | | |
| | | |
+----+----+----+
| | | |
1 | | O | X |
+----+----+----+
| | | |
2 | X | | |
+---------------
So I'd like to know why this happens?
Here is my code:
void compMove(char board[][columns])
{
int randomNum, randomNum1, i, j;
bool didMove = FALSE;
srand((int)time(NULL)); //Seed the random number generator
randomNum = rand() % 3;
randomNum1 = rand() % 3;
while(board[randomNum][randomNum1] != ' ')
{
randomNum = rand() % 3;
randomNum1 = rand() % 3;
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
if(i == randomNum && j == randomNum1)
{
board[i][j] = 'X';
didMove = TRUE;
break;
}
}
if(didMove == TRUE) break;
}
}
You do not need a loop to set movement on the board.
board[randomNum][randomNum1] = 'X';
is enough to set.
And you probably call this function twice, because function plays only one move.
I suspect your checkWin function is the culprit. Probably has an = somewhere you meant to have an ==. But that's just a guess without seeing it.
Your compMove function is grossly overcomplicated, but not wrong. Here's code that does the same thing:
void compMove(char board[][columns]) {
int row, col;
/* DON'T seed RNG here; do it once in main() */
do {
row = rand() % 3;
col = rand() % 3;
} while (board[row][col] != ' ');
board[row][col] = 'X';
}

Same Code Giving Different Output in C

I am stumped by this. I am pasting the program below.
void printGrid(int n,char grid[n][n]){
for(int i = 0; i<n ; i ++) {
printf("\n%s",grid[i]);
}
}
int main() {
char grid[6][6]= {"-----","-----","-----","-----","-----"};
printGrid(5, grid);
for(int i = 0; i<5 ; i ++) {
printf("\n%s",grid[i]);
}
return 0;
}
Output:
-----
-
--
---
-----
-----
-----
-----
-----
Why does the same for loop produce different output outside and inside the function printGrid?
You re using a VLA but the size does not match the dimensions of the array you passed in. When you have an array:
char a[m][n];
The char at a[x][y] is found essentially by a + x*m + y. Moreover, the layout of the array you made in memory looks like this:
-----\0-----\0-----\0-----\0-----\0-----\0
But since your first dimension is 5 instead of 6, when you index each row you are hitting it like this:
-----\0-----\0-----\0-----\0-----\0-----\0
^ | | | | | |
^ | | | | |
^ | | | |
^ | | |
^ | |
^ |
^
(your loop doesn't actually print the last two). If you instead call it like:
printGrid(6, grid);
you will see the output is more what you expect because the strides line up. The other loop should probably be using 6 as well.

Resources