Function error: expected ')' before 'char' - c

This is a program to create a table full of points, but i'm trying to separate in functions.
I am doing this because I will need to add more functions in the future using the variables x and tabuleiro. I'm getting the error in the title and I don't understand why. Can you guys help me?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char tabuleiro_init(int dim, char tabuleiro[15][15]);
int main(int x)
{
printf("Put the dimension of the table: ");
scanf("%d", &x);
char tabuleiro[15][15];
tabuleiro_init(x, tabuleiro[15][15]);
}
char tabuleiro_init(dim, char tabuleiro)
{
if (dim >= 7 && dim <= 15 && dim%2 != 0)
{
for (int i = 0; i < dim; i++)
{
for (int j = 0; j < dim; j++)
{
printf(".", tabuleiro[i][j]);
printf(" ");
}
printf("\n");
}
}
}

There is a mismatch between the declaration and the definition of your tabuleiro_init function.
Compare
char tabuleiro_init(int dim, char tabuleiro[15][15])
with
char tabuleiro_init(dim, char tabuleiro)
For the first parameter, you are missing int in the definition.
For the second parameter, you declared it to be char tabuleiro[][15], or a pointer to a char array of 15 elements, but you defined it to be just a char.

For starters this declaration of main
int main(int x)
is incorrect.
Declare the function like
int main( void )
and within the function declare the variable x like
int x;
In this call
tabuleiro_init(x, tabuleiro[15][15]);
the second argument is a non-existent element of the array of the type char
Instead write
tabuleiro_init(x, tabuleiro);
The function declaration in its definition does not correspond to the first function declaration
char tabuleiro_init(dim, char tabuleiro)
And the first parameter does not have a type specifier.
Also the return type char of the function dies not make a sense and moreover the function returns nothing.
So at least use the following function declaration in the both cases
void tabuleiro_init(int dim, char tabuleiro[15][15])
In this call of printf
printf(".", tabuleiro[i][j]);
the second argument is not used. Maybe you just mean
printf("." );
(that does not maje a great sense) or
printf(".%c", tabuleiro[i][j]);
but in the last case the array must be initialized before passing it to the function.

Related

Changing the value of an element in a struct

I'm new to structs. I am trying to write a program that has a struct, and the struct is supposed to store a character array and its length. I want to be able change the length's value as I would be creating functions like trimming/concatenating the array. Here is a code I wrote:
#include <stdio.h>
#include <stdlib.h>
struct strstruct{
unsigned int length;
char string[20];
};
typedef struct strstruct stru;
int strleng(stru A){
int i=0;
while(A.string[i]!='\0'){
i++;
}
A.length =i;
return i;
}
int main(){
stru A = {1,
{'a','b','c','d','e','f'}
};
printf("%d %d\n",strleng(A),A.length);
return 0;
}
The value of A.length is not changing inspite of calling strleng.
(i)Why?
(ii) Is there another way to do it?
For starters the order of evaluation of arguments in a function call is unspecified.
So in this call
printf("%d %d\n",strleng(A),A.length);
the evaluation of the argument expression A.length can occur before calling the function strleng or vice versa.
Secondly the function strleng declared like
int strleng(stru A);
deals with a copy of the original object A declared in main and used as an argument. So changing the copy does not influence on the original object.
You need to pass the object by reference through a pointer to it.
unsigned int strleng( stru *A){
unsigned int i=0;
while(A->string[i]!='\0'){
i++;
}
A->length =i;
return i;
}
and in main you should write for example
unsigned int n = strleng( &A );
printf("%u %u\n", n, A.length );
Pay attention to that on one hand, the data member length is declared as having the type unsigned int
unsigned int length;
On the other hand, within your original function strleng you are using an object of the signed type int and the function return type is also int. The function should use at least the same type unsigned int instead of the type int.
Try the code below:
#include <stdio.h>
#include <stdlib.h>
struct strstruct{
unsigned int length;
char string[20];
};
typedef struct strstruct stru;
int strleng(stru* A){
int i=0;
while(A->string[i]!='\0'){
i++;
}
A->length =i;
return i;
}
int main(){
stru A = {1,
{'a','b','c','d','e','f'}
};
printf("%d %d %d\n",A.length, strleng(&A),A.length);
printf("%d \n",A.length);
return 0;
}
You will get output: 6 6 1. I should get the answer now.
At first, you need to use pointer as a parameter if you want to modify struture's value inner a fucntion.
For your question:
To most of the c compiler, the functions inner a printf function is prcessed from right to left. I think the compiler in your case is this one.
For some c compiler, it do process functions in one line from left to right.
I hope it can help you, c online compiler: https://www.onlinegdb.com/online_c_compiler.
printf("%d %d\n",strleng(A),A.length);
Firstly, Here you are passing the argument to the strleng function as a value means strleng function' parameter is a copy of A. In other words, variable A in the main function and structure variable inside the strleng function are two independent variables. So changing A.length in the strleng function will not be visible to your variable A in the main function. (There are many good online resources available about Pass by value vs. Pass by reference. You can check those for better understanding)
Most of the compilers takes each parameter of printf() from right to left. So here A.length execute first then strleng(A). So even you pass the argument by reference, it will still output 6 1.
Updated Code
#include <stdio.h>
#include <stdlib.h>
struct strstruct {
unsigned int length;
char string[20];
};
typedef struct strstruct stru;
int strleng(stru* A) {
int i = 0;
while(A->string[i] != '\0'){
i++;
}
A->length = i;
return i;
}
int main() {
stru A = {1, {'a','b','c','d','e','f'}};
printf("%d %d %d\n", A.length, strleng(&A), A.length);//6 6 1
return 0;
}

Multidimensional arrays in functions in c

I have this exercise to make a transpose of a matrix in C. I made a function to check for type n*n but when I'm trying to ask the user for the matrix I don't know how I should declare the array. And I'm getting this compile error "type of formal parameter 1 is incomplete" in the function on the [n2] part.
The parameters of the functions for multi dimensional arrays shouldn't be like this -> int matrix[][n2]. or is cause i'm using a variable and not a constant or a pre defined size. ?
#include <stdio.h>
#define prompt "Dimenção da matriz (nxn) >>"
#define prompt_1 "Introduza os valores : "
void getType( int *n1, int *n2 );
void getMatrix( int matrix[][n2], int lim1, int lim2);
//void trans(int matrix[][n2]);
int main(int argc, char const *argv[]) {
int n1, n2;
getType(&n1, &n2);
int matrix[n1][n2];
//printf("%dx%d\n", n1, n2);
getMatrix(matrix, n1, n2);
//trans(matrix);
return 0;
}
void getType(int *n1, int *n2){
printf("%s", prompt );
scanf("%dx%d", &(*n1), &(*n2));
}
void getMatrix( int matrix[][n2], int lim1, int lim2){
printf("%s\n", prompt_1 );
for(int line = 0; line < lim1; line++ ){
for(int column = 0; column < lim2; column++){
printf("Linha %d coluna %d ->", line, column );
scanf("%d", &matrix[line][column]);
}
}
}
The signature should be:
void getMatrix( int lim1, int lim2, int matrix[lim1][lim2] )
You are allowed omit the lim1 inside square brackets but it is good documentation to include it.
The main point is that the variable inside the square brackets must either be a parameter from earlier in the parameter list, or some other variable in scope (which can only be a global variable, but that's usually a bad idea).
Also it would be good to check scanf return value otherwise you may create matrix with garbage dimension.

"error: expected ';', ',' or ')' before numeric constant" is coming up to my code

The program is about function making an average. I get an error:
error: expected ';', ',' or ') before numeric constant
within the avg_array() function whenever I build it. Help would be appreciated, thanks!
#include <stdio.h>
#define SIZE 5
// Prototypes
int avg_array (int*, int);
main()
{
int values[SIZE];
int avg;
int i;
printf("Enter 5 numbers please. \n");
for(i=0; i<SIZE; i++)
{
scanf("%d", &values[i]);
}
avg = avg_array(values, SIZE);
printf("\n The avg of the array is %d \n", avg);
getchar();
getchar();
} // end main()
/* Implement avg_array() WHERE THE ERROR PERTAINS */
avg_array(int my_array[], int SIZE)
{
int sum;
int i;
int fxn_average;
for(i=0; i<SIZE; i++)
{
sum = sum + my_array[i];
}
fxn_average = (sum/SIZE);
return (fxn_average);
}
You are using the identifier SIZE as an argument. This is also a macro that gets converted to 5 by the preprocessor. After the preprocessor applies the macros, it would look like
avg_array (int my_array[], int 5)
Since 5 is a numeric constant instead of an identifier, it generates an error. Change the variable name.
It looks like you also have a function signature missing a return type, which should match the prototype declared above. Try this instead:
int avg_array (int *my_array, int size)
{
int sum = 0;
int i;
for(i=0; i<size; i++)
{
sum = sum + my_array[i];
}
return sum/size;
}
The variable sum should be initialized to 0. The local variable fxn_average is not needed because you can use just return sum/size; at the end instead.
I changed the type of the first argument from int[] (array of int) to int * (pointer to int) so the function definition matches the prototype given in the question. The function was declared as
int avg_array (int*, int);
These arguments have no identifiers; only their types are specified. This is valid C, but many style guides prescribe against it since naming arguments helps the reader understand meaning or intent. If you are writing a programming interface, for example, all the programmer will likely see is the function prototypes in a header file. It must be clear what the arguments are to write a correct function call. Anyway, adding in identifiers looks like:
int avg_array (int *my_array, int size);
which is the same as in the definition I used above.

creating word search puzzle solver in c, Incompatible pointer type and pointer from integer without cast warnings in c

as the title says im attempting to create a program that searches through a 3x4 predefined grid for words that the user enters in the command line, i have the code completed but i am encountering these pointer warnings when i try to compile, the errors are as follow:
In function 'main':
59: warning: passing argument 1 of 'horizontalrow' makes pointer from integer without a cast
59: warning: passing argument 2 of 'horizontalrow' from incompatible pointer type
60: warning: passing argument 1 of 'verticalrow' from incompatible pointer type
60: warning: passing argument 2 of 'verticalrow' from incompatible pointer type
61: warning: passing argument 1 of 'diagonalrow' from incompatible pointer type
61: warning: passing argument 2 of 'diagonalrow' from incompatible pointer type
my code is as follows:
#include<stdio.h>
#include<string.h>
#define ROW 3
#define COL 4
void horizontalrow(char *a[ROW][COL], char *argv[], int argc)
{
int i=0,j=0, k =0;
for(i = 0; i < ROW - 1; i++){
for(j = 0;j < COL; j++){
for(k=0;k<argc;k++){
if(strcmp(a[i][j],argv[k])!=0){
printf("%s appears horizontally at a[%d][%d]", a[i][j],i,j);
}
}
}
}
}
void verticalrow(char *a[ROW][COL], char *argv[], int argc)
{
int i=0,j=0, k=0;
for(j = 0; j< COL - 1; j++){
for(i = 0; i < ROW; i++){
for(k=0;k<argc;k++){
if(strcmp(a[i][j], argv[k])!=0){
printf("%s found veritcally at a[%d][%d]",a[i][j],i,j);
}
}
}
}
}
void diagonalrow(char *a[ROW][COL], char *argv[], int argc)
{
int slice = 0, i = 0, j =0, z1 = 0, z2 =0, k=0;
for(slice = 0; slice < ROW + COL -1; slice++){
for(j = z2; j <= slice - z2; j++){
for(k=0;k<argc;k++){
if(strcmp(a[i][j],argv[k])!=0){
printf("%s is found diagonally at a[%d][%d]", a[i][j], i, j);
}
}
}
}
}
int main(int argc, char *argv[])
{
int i=0,j=0,k=0;
char a[ROW][COL] ={ {'a','b','c','d'},
{'d','c','b','a'},
{'x','y','z','d'}};
horizontalrow(a[ROW][COL],argv[k], argc);
verticalrow(a[ROW],argv[k], argc);
diagonalrow(a[ROW],argv[k], argc);
return 0;
}
There are too many errors in your code. Here's a working program to get you going in the right direction. I have removed verticalrow and diagonalrow to simplify things.
#include <stdio.h>
#include <string.h>
#define ROW 3
#define COL 4
void horizontalrow(char a[ROW][COL], char *argv[], int argc)
{
int i=0,j=0, k =0;
for(i = 0; i < ROW - 1; i++)
{
for(j = 0;j < COL; j++)
{
for(k=0;k<argc;k++)
{
if(a[i][j] == *argv[k] )
{
printf("%c appears horizontally at a[%d][%d]\n", a[i][j], i, j);
}
}
}
}
}
int main(int argc, char* argv[])
{
char a[ROW][COL] = { {'a','b','c','d'},
{'d','c','b','a'},
{'x','y','z','d'} };
horizontalrow(a, argv+1, argc-1);
return 0;
}
Changes made to your program
Signature of the function.
You had:
void horizontalrow(char *a[ROW][COL], char *argv[], int argc)
I changed it to
void horizontalrow(char a[ROW][COL], char *argv[], int argc)
// ^^^ no pointer, just the array.
You declaration expects the argument to be a two dimensional array of pointers to char. What you need is just a two dimensional array chars.
Comparing the command line arguments.
You had:
if(strcmp(a[i][j],argv[k])!=0){
I changed it to:
if(a[i][j] == *argv[k] )
Your code was syntactically correct but didn't make any sense semantically. My changes assume that the command line arguments you use to run the program are single character strings, such as
myprogram a b c
If you had a different idea for the command line arguments, you'll need to adapt the code.
Format specifier in printf.
You had:
printf("%s appears horizontally at a[%d][%d]", a[i][j],i,j);
I changed it to:
printf("%c appears horizontally at a[%d][%d]\n", a[i][j], i, j);
You had a syntactically valid format specifier. Since I changed the argument type, I had to change the format specifier too.
Removed unnecessary variables from main.
I removed the line
int i=0,j=0,k=0;
You don't need these variables.
Changed the syntax used to call the function.
You had:
horizontalrow(a[ROW][COL],argv[k], argc);
I changed it to:
horizontalrow(a, argv+1, argc-1);
Use of a[ROW][COL] as an argument calls the function by using the value obtained from evaluating a[ROW][COL]. It is syntactically wrong. The expected type of the function does not match the value type. The value type is char. Also, evaluation of a[ROW][COL] leads to undefined behavior since you are accessing the array out of bounds. The valid range of indices to access a are a[0][0] through a[ROW-1][COL-1].
It's not clear to me what you were hoping to pass to the function by using argv[k]. The value type of argv[k] does not match the argument type of the function. You can read more about using command line arguments at http://en.cppreference.com/w/c/language/main_function. Hopefully when you have finished reading that page, you will understand why I am using argc-1 as the third argument to horizontalrow.

Pointers for a beginner (with code)

I am doing my first ever homework assignment in C and I'm trying to grasp pointers. They make sense in theory, but in execution I'm a little fuzzy. I have this code, which is supposed to take an integer x, find its least significant byte, and replace y with that byte in the same location. GCC returns with:
"2.59.c:34:2: warning: passing argument 1 of ‘replace_with_lowest_byte_in_x’ makes pointer from integer without a cast [enabled by default]
2.59.c:15:6: note: expected ‘byte_pointer’ but argument is of type ‘int’"
And the same for argument 2. Would someone be so kind as to explain to me what is going on here?
#include <stdio.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, int length) {
int i;
for (i=0; i < length; i++) {
printf(" %.2x", start[i]);
}
printf("\n");
}
void replace_with_lowest_byte_in_x(byte_pointer x, byte_pointer y) {
int length = sizeof(int);
show_bytes(x, length);
show_bytes(y, length);
int i;
int lowest;
lowest = x[0];
for (i=0; i < length; i++) {
if (x[i] < x[lowest]) {
lowest = i;
}
}
y[lowest] = x[lowest];
show_bytes(y, length);
}
int main(void) {
replace_with_lowest_byte_in_x(12345,54321);
return 0;
}
The function expects two pointers but you're passing integer(-constant)s. What you probably want is to put the numbers in their own variables and pass the addresses of those to the function: (in main):
int a = 12345, b = 54321;
replace_with_lowest_byte_in_x(&a, &b);
Note that you're still passing incompatible pointers.
The compiler is right, your replace_with_lowest_byte_in_x() expects two unsigned char *, but you pass two ints to it. Yes, the ints can be regarded as memory address, but it's dangerous, so there is a warning. &variable gives you the address of variable.

Resources