error: assignment makes integer from pointer without a cast (using gets) - c

I'm trying to get an input from the user and store it in a certain place in a 2d Array called Matrix. The Compiler says:
error: assignment makes integer from pointer without a cast [-Werror]
Matrix[i][j]=matrixinput;
This is the part of my Program:
int i=0;
int j=0;
char size[1];
char matrixinput[1];
int sizeint=0;
int Matrix[10][10];
while (i<=(sizeint-1) && j<=(sizeint-1)){
gets(matrixinput);
Matrix[i][j]=matrixinput;
j++;
if (j==(sizeint-1)){
j=0;
i++; }
else {continue;};
};
I am just a Student with basically 0 expeience in c or any kind of programming, so maybe somebody can help me to find out how I can fix this.

Matrix[i][j]=matrixinput;
What you are doing is assign a char array of size 1 to an int. Your compiler issues a warning for this.
Don't assign arrays like this .
You want to take input in 2-d array, directly take inout in it using scanf -
scanf("%d",&Matrix[i][j]);
And this loop -
while (i<=(sizeint-1) && j<=(sizeint-1)){
This loop won't iterate for once , because condition will remain always flase as sizeint-1 is -1 as sizeint is -1 . So , you need to work on this condition also .
Note - Increase your array's size and don't use gets , use fgets instead.

A professor gave me good tip for debug this type of error, ask you if the left operator type is equals to the right operator type. in your code it's false because right operator equals integer and left operator equals character.

What you could do to remove the error is: Matrix[i][j] = (int) matrixinput[0]; (Array indexing starts with 0)
But your code will still not work because you have initialized i and j to 0 and also sizeint. It means that, in the loop while (i<=(sizeint-1) && j<=(sizeint-1)), you are basically checking if 0 <= -1 which is impossible and so your while loop will never execute. What you could do is:
gets(matrixinput);
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
if(i == 0 && j == 0) { //say you want to put the char at the first row first column
Matrix[i][j] = matrixinput[0];
}
}
}
You could also use scanf() instead of gets().

here is one possible correction to the posted code
It removes unneeded clutter from the code
It makes use of scanf() so no char to int conversion needed in the code
It consistently indents the code
It #defines the size of the matrix to make the code clearer
Note: it fails to check the returned value (not the parameter value) from scanf() to assure the input/conversion was successful
#define MATRIX_SIZE (10)
// int i=0;
// int j=0;
// char size[1];
// char matrixinput[1];
// int sizeint=0;
//int Matrix[10][10];
int Matrix[MATRIX_SIZE][MATRIX_SIZE];
// while (i<=(sizeint-1) && j<=(sizeint-1)){
// gets(matrixinput);
// Matrix[i][j]=matrixinput;
// j++;
// if (j==(sizeint-1)){
// j=0;
// i++; }
// else {continue;};
//};
for( int i=0; i<MATRIX_SIZE; i++)
{
for( int j=0; j<MATRIX_SIZE; j++)
{
scanf( "%d", Matrix[i][j]);
}
}

Related

Storing multiple strings In C

How can multiple strings be stored in C?
if we consider that the number of strings are taken from user , how can we save them in c language , considering that we had not declared any char strings before asking the number of strings from user , because the number of strings was not available.here is what i did but i ended up printing the first character of strings.
#include <stdio.h>
int main()
{
int a;
scanf("%d",&a);
char array[a][1];
for(int i = 0 ; i < a ; i++)
{
for(int j = 0 ; j < 1 ; j++)
{
scanf("%s",&array[i][j]);
}
}
for(int i = 0 ; i < a ; i++)
{
for(int j = 0 ; j < 1 ; j++)
{
printf("%c",array[i][j]);
}
printf("\n");
}
}
Let's start from the beginning.
I don't think you really understand what is char though. Char is only ONE character. So things like char array[16][1] mean that you have array of 16 strings where each string have maximum length of one. Also your next step scanf("%s", &array[i][j]); doesn't make sense, since you're getting multiple symbols as an input, but you're writing only to single character. Proper solution would be something like this:
char array[a][255]; // 255 will be maximum length of one 'string'
for(int i = 0; i < a; ++i) {
scanf("%s", array[i]);
}
As you can see, you don't need & sign here, because array[i] already returns address of the first character in the string. The same thing applies to printing. Proper way is to do following thing:
for(int i = 0; i < a; ++i) {
printf("%s\n", array[i]);
}
Your solution only displays one character.
And remember, char is just basic number, ranging typically from 0-255 (if compiler defaults char to unsigned char). Your code implies that you treat char as a full C++ string, which it definitely isn't.

Filling and Printing a 2D array

So I have a 2D array that I want to use later. Right now I just want to fill the empty spots.
So far I've just been messing around with array types and different default values. From my understanding a new array is filled with '0', I have tried NULL aswell.
int r = 5;
int c = 5;
int i;
int j;
int k = 0;
int area = r*c;
const char *array[r][c]; //tried char array[r][c] and other types
Setup my initial values and array here.
while(k< area){
for (j = 0; j < c; j++){
for (i = 0; i<r; i++){
if (array[i][j] == 0){
board[i][j] = ".";
}
printf("%c",aray[i][j]);
if (i = r - 1){
printf("\n");
}
k++;
}
}
}
This is where I try replacing all non filled values (all of them at this point) with ".", so the output should be a row of 5x5 dots. Instead I get weird letters and numbers. I have tried %s insead of %c, and no luck there but the output was different. Where I do %s I get some dots, but still not on a grid and the weird values show up.
Also Im pretty sure printf in a for loop, by default does it on a new line so I won't get the grid, so is there a better way of doing this?
What you have is an array of pointers. This would be suitable for a 2D array of strings, but not for a 2D array of characters. This isn't clear from your question, so I'll assume that you actually want a 2D array of characters. The syntax is: char array [r][c];.
Notably, since you used r and c which are run-time variables, this array is a variable-length array (VLA). Such an array cannot be placed at file scope ("global"). Place the array inside a function like main().
In order to use VLA you must also have a standard C compiler. C++ compilers and dinosaur compilers won't work.
Since you will have to declare the VLA inside a function, it gets "automatic storage duration". Meaning it is not initialized to zero automatically. You have to do this yourself, if needed: memset(array, 0, sizeof array);. But you probably want to initialize it to some specific character instead of 0.
Example:
#include <stdio.h>
#include <string.h>
int main (void)
{
int r = 5;
int c = 5;
char array [r][c];
memset(array, '#', sizeof array);
for(size_t i=0; i<r; i++)
{
for(size_t j=0; j<c; j++)
{
printf("%c", array[i][j]);
}
printf("\n");
}
}
Output:
#####
#####
#####
#####
#####
From my understanding a new array is filled with '0'
const char *array[r][c];
No*, you have fill it yourself in a double for loop, like this:
for(int i = 0; i < r; ++i)
for(int j = 0; j < c; ++j)
array[i][j] = 0
since your structure is a variable sized array.
Instead I get weird letters and numbers
This happens because your code invokes Undefined Behavior (UB).
In particular, your array is uninitialized, you then try to assign cells to the dot character, if their value is already 0.
Since the array is not initialized, its cells' values are junk, so none satisfied the condition of been equal to 0, thus none was assigned with the dot character.
You then print the array, which still contains garbage values (since it was never really initialized by you), and the output is garbage values.
* As stated by #hyde, this is true for local non-static arrays (which is most probably your case). Statics and globals are default initialized (to zero if that was the case here).
You have several problems:
You are declaring a pointer to the array you want, not the array
Whenever R and C are not compile time known, you can't use a built in array. You might can however use VLAs (C99 as only C standard has VLAs mandatory, C11 made them optional again), which seems like a built in array with a size not known at compile time, but has very important implications, see : https://stackoverflow.com/a/54163435/3537677
Your array is only zero filled, when declared as a static variable.
You seem to have mistake the assign = operator with the equal == operator
So by guessing what you want:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define R 5
#define C 5
int r = R;
int c = C;
int i;
int j;
int k = 0;
int area = R*C;
const char array[R][C];
int main() {
while(k< area){
for (j = 0; j < c; j++){
for (i = 0; i<r; i++){
if (array[i][j] == 0){
}
printf("%c",array[i][j]);
if (i == r - 1){
printf("\n");
}
k++;
}
}
}
//or
char** dynamic_array = malloc(r * c);
if (dynamic_array == NULL) {
perror("Malloc of dynamic array failed");
return EXIT_FAILURE;
}
memset(dynamic_array, '0', r*c);
k = 0;
while(k< area){
for (j = 0; j < c; j++){
for (i = 0; i<r; i++){
if (dynamic_array[i][j] == 0){
}
printf("%c",dynamic_array[i][j]);
if (i == r - 1){
printf("\n");
}
k++;
}
}
}
return 0;
}

C - Passing a 3D arrays of chars to a function

I'm trying to write a program that analyzes a (3 x 4) matrix of strings provided by the user. Ultimately, it needs to output the longest string present in the matrix, along with that string's length.
My program seems to read the input correctly, as judged its success in echoing back the input strings, but it does not correctly output the longest word. I'm sure I'm committing some kind of pointer-related error when I pass the value of longest word, but I do not have any idea how to solve it.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define M 4
#define N 5
#define MAX_DIM 20
void findMAX(char matrice[N][M][MAX_DIM]) {
char maxr;
int index;
int i, j, it;
index = 0;
maxr = *(*(*(matrice+0)+0)+MAX_DIM);
for (i = 0; i < N-1; i++) {
for (j = 0; j < M-1; j++) {
if (index < strlen(matrice[i][j])) {
index = strlen(matrice[i][j]);
// I save the longer line's value
it = i;
// I save the maximum's value
maxr = *(*(*(matrice+i)+j)+MAX_DIM);
}
}
}
printf ("The MAX is: -/%s/- and it's long: -/%d/- \n", maxr, index);
printf ("It is content in the: %d line, which is: \n", it);
for (j = 0; j < N-1; j++) {
printf("%s ", matrice[it][j]);
}
}
void leggi(char matrice[N][M][MAX_DIM]) {
int i, j;
for (i = 0; i < N-1; i++) {
for (j = 0; j < M-1; j++) {
printf ("Insert the element matrix [%d][%d]: ", i, j);
scanf ("%s", matrice[i][j]);
fflush(stdin);
}
}
}
void stampa(char matrice[N][M][MAX_DIM]) {
int i, j;
printf("\n(4 x 3) MATRIX\n");
for (i = 0; i < N-1; i++) {
for (j = 0; j < M-1; j++) {
printf("%s ", matrice[i][j]);
}
printf("\n\n");
}
}
int main(int argc, char *argv[]) {
char matrix[N][M][MAX_DIM]; //Matrix of N*M strings, which are long MAX_DIM
printf("************************************************\n");
printf("** FIND THE LINE WITH THE MAXIMUM ELEMENT **\n");
printf("** IN A (4 x 3) MATRIX **\n");
printf("************************************************\n");
printf ("Matrix Reading & Printing\n");
leggi (matrix);
stampa (matrix);
findMAX(matrix);
return 0;
}
First of all to address some misconceptions conveyed by another answer, consider your 3D array declared as
char matrix[N][M][MAX_DIM];
, where N, M, and MAX_DIM are macros expanding to integer constants.
This is an ordinary array (not a variable-length array).
If you want to pass this array to a function, it is perfectly acceptable to declare the corresponding function parameter exactly the same way as you've declared the array, as indeed you do:
void findMAX(char matrice[N][M][MAX_DIM])
But it is true that what is actually passed is not the array itself, but a pointer to its first element (by which all other elements can also be accessed. In C, multidimensional arrays are arrays of arrays, so the first element of a three-dimensional array is a two-dimensional array. In any case, that function declaration is equivalent to both of these:
void findMAX(char (*matrice)[M][MAX_DIM])
void findMAX(char matrice[][M][MAX_DIM])
Note in particular that the first dimension is not conveyed. Of those three equivalent forms, I find the last clearest in most cases.
It is quite odd, though, the way you access array elements in your findMAX() function. Here is the prototypical example of what you do:
maxr = *(*(*(matrice+i)+j)+MAX_DIM);
But what an ugly and confusing expression that is, especially compared to this guaranteed-equivalent one:
maxr = matrice[i][j][MAX_DIM];
Looking at that however, and it how you are using it, I find that although the assignment is type-correct, you are probably using the wrong type. maxr holds a single char. If you mean it to somehow capture the value of a whole string, then you need to declare it either as an array (into which you will copy strings' contents as needed), or as a pointer that you will set to point to the string of interest. The latter approach is more efficient, and I see nothing to recommend the former for your particular usage.
Thus, I think you want
char *maxr;
... and later ...
maxr = matrice[0][0];
... and ...
maxr = matrice[i][j];
That sort of usage should be familiar to you from, for example, your function stampo(); the primary difference is that now you're assigning the expression to a variable instead of passing it directly to a function.
And it turns out that changing maxr's type that way will correct the real problem here, which #AnttiHaapala already pointed out in comments: this function call ...
printf ("The MAX is: -/%s/- and it's long: -/%d/- \n", maxr, index);
requires the second argument (maxr) to be a pointer to a null-terminated array of char in order to correspond to the %s directive in the format string. Before, you were passing a single char instead, but with this correction you should get mostly the expected result.
You will probably, however, see at least one additional anomaly. You final loop in that function has the wrong bound. You are iterating with j, which is used as an index for the second dimension of your array. That dimension's extent is M, but the loop runs to N - 1.
Finally, I should observe that it's odd that you allocate space for a 5 x 4 array (of char arrays) and then ignore the last row and column. But that's merely wasteful, not wrong.
Try something like this:
void findMAX(char matrice[N][M][MAX_DIM]){
// char maxr
char maxr[MAX_DIM];
int index;
int i, j, it;
index = 0;
// maxr = *(*(*(matrice+0)+0)+MAX_DIM);
strncpy(maxr, *(*(matrice+0)+0), MAX_DIM);
for (i = 0; i < N-1; i++)
{
for (j = 0; j < M-1; j++)
{
if (index < strlen(matrice[i][j]))
{
index = strlen(matrice[i][j]);
it = i;
// maxr = *(*(*(matrice+i)+j)+MAX_DIM);
strncpy(maxr, *(*(matrice+i)+j), MAX_DIM);
}
}
}
printf ("The MAX is: -/%s/- and it's long: -/%d/- \n", maxr, index);
printf ("It is content in the: %d line, which is: \n", it);
// for (j = 0; j < N-1; j++){
for (j = 0; j < M-1; j++){
printf("%s ", matrice[it][j]);
}
}
It's possible to pass multi-dimensional arrays to C functions if the size of the minor dimensions is known at compile time. However the syntax is unacceptable
void foo( int (*array2d)[6] )
Often array dimensions aren't known at compile time and it is necessary to create a flat array and access via
array2D[y*width+x]
Generally it's easier just to use this method even if array dimensions are known.
To clarify in response to a comment, C99 allows passing of variable size arrays using the more intuitive syntax. However the standard isn't supported by Microsoft's Visual C++ compiler, which means that you can't use it for many practical purposes.

Can you use double brackets in a nested for loop?

What I mean by my question is that if I have a nested for loop like
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; i++)
{
printf("%d\n"___);
}
}
What would I put in the blank? Would [i][j] be illegal if I declared an array already?
I am not sure what exactly you are stuck on based on your question so I made a minimal C program with comments
I declared an int array who's first and second dimension are at least 10 because you iterate both i and j from 0 to 9 (inclusive). This is to avoid out of bounds problems while iterating
The array's elements are not initialized in the program. It is possible that this program will print all zeros when you run it. It is also possible that it prints other values that happened to be in memory (because the array values are not initialized)
Last I declared i and j outside the for loop just in case this was the problem you were facing
#include <stdio.h>
int main(int argc, char** argv) {
// Declare an array
// Note that both dimensions are at least 10
// Also note that none of the values are initialized here
int myArray[10][10];
// Both i and j are declared here rather than in the for loop
// This is to avoid the following potential error:
// error: 'for' loop initial declarations are only allowed in C99 or C11 mode
int i, j;
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
// Note that the values this prints are uninitialized
printf("%d\n", myArray[i][j]);
}
}
return 0;
}
Your question was really unclear. But from what I understand is you have some 2-d array, and you want to print contents of array.
You must have arrary already defined as int arr[10][10], then you can use,
printf("%d\n", arr[i][j]);

Trying to find if the element of an int array is equal to a number entered in C

I'm trying to use a for loop to iterate over the elements of an integer array and find out if a particular element in that array is equal to some other integer.
Here's what I've got, and it doesn't seem to work:
int squaresArray[1000];
int numberOfSquares = 1000;
int i = 0;
for (i; i<=numberOfSquares; i++)
{
squaresArray[i] = i*i;
if (number == squaresArray[i]){
printf("%d is a perfect square\n", number);}
break;
}
According to what I know about for loops this should work, but it prints nothing even when the number should be equal to some element of the array.
You're breaking on the first iteration due to misplaced brackets (i.e. break is not in the scope of the if-statement). Change it to:
if (number == squaresArray[i]) {
printf("%d is a perfect square\n", number); // no closing bracket here
break;
} // <--
Also, your loop condition should probably be i < numberOfSquares. After all, numberOfSquares (1000) is an out of bounds index for an array of length 1000. Moreover, you don't need a loop initialization statement if you've already declared/initialized i in the loop's enclosing scope. Hence, we're left with
int i = 0;
for (; i < numberOfSquares; i++)
If you're using C99 and above, you can limit the scope of i to only the loop, which should be preferred:
for (int i = 0; i < numberOfSquares; i++)

Resources