This is my code and it is not working.
It generate these errors when I pass char array like this grid[r][c]
[Error] use of parameter 'r' outside function body
[Error] use of parameter 'c' outside function body
It generate these errors when I pass char array like this grid[][c]
[Error] use of parameter 'c' outside function body
It generate these errors when I pass char array like this grid[][]
[Error] declaration of 'grid' as multidimensional array must have bounds for all dimensions except the first
And it runs perfectly fine when I pass this like this grid[1][2] i.e. just passing with an integer.0
I am stuck here and I don't know what to do or what not??
How to get rid of this problem?? Help Me !!!
Thanks in Advance!
void dfs(int r, int c, int pacman_r, int pacman_c, int food_r, int food_c, char grid[r][c]) {
//logic here
}
int main(void) {
int r, c;
int pacman_r, pacman_c;
int food_r, food_c;
scanf( "%d %d", &pacman_r, &pacman_c);
scanf( "%d %d", &food_r, &food_c);
scanf( "%d %d", &r, &c);
char grid[r][c];
for( int i=0; i<r; i++) {
scanf("%s[^\\n]%*c", grid[i]);
}
dfs( r, c, pacman_r, pacman_c, food_r, food_c, grid);
return 0;
}
you should pass the argument as a char* then work with it as a pointer to a flattened instance of your array
void fn(char* grid, int c){
printf("%c", (grid+n*c)[m]);
}
this will print `grid[n][m]
Related
So i'm trying to pass
char parent[n][50];
into a function initialize();
And then copy the char x, into the parent [ i ] inside the initialize(); function. Example
x = "Cityname"
and when passed into the initialize();
it would do
strcpy(parent[i], x);
to make the
parent[i] = "Cityname"
void initialize(char *parent, int *ranks, char x, int i){
strcpy(parent[i], x);
ranks[i] = '0';
}
int main(){
int n, i = 1;
char x[20];
printf("Enter how many city are there : "); scanf("%d", &n); fflush(stdin);
char parent[n][20];
int ranks[n];
while(1){
printf("enter city name: "); scanf("%[^\n]", x);
if(i <= n){
initialize(parent[][20], ranks, x, i);
i++;
} else {
printf("The city is at maximum\n");
}
}
}
It tells a warning:
passing argument 1 of 'strcpy' makes pointer from integer without a cast
note: expected 'char *' but argument is of type 'char'
and also in function main
error: expected expression before ']' token
Can anyone explain how to strcpy(parent[i], x) correctly? I can't seem to figure this problem out.
I see several problems with your code. Arrays vs pointers in C can be confusing, so there are a few rules to keep in mind:
char x[n] can be automatically converted by the C compiler to char *x.
char x[10][20] is represented under the hood as a 1D array, and the compiler computes the offsets behind the scenes. For example, if x were a 10 x 20 array, the expression x[1][2] could be compiled as *(x + 22). For this reason, it can cause surprising results to cast a 2D array to a char*, and it is invalid to cast a 2D array to a char**.
With these rules in mind, here is how I would change your code
void initialize(char (*parent)[20], int *ranks, char *x, int i){
strcpy(parent[i], x);
ranks[i] = '0'; // Did you want an automatic conversion from char to int here? Maybe you meant ranks[i] = 0?
}
int main(){
int n, i = 0; // As Craig mentions, i should start at 0.
char x[20];
printf("Enter how many city are there : "); scanf("%d", &n); fflush(stdin);
char parent[n][20];
int ranks[n];
while(1){
printf("enter city name: "); scanf("%19s", x); // scanf will automatically stop at whitespace, and you must include the max length to avoid a buffer overrun.
if(i < n){
initialize(parent, ranks, x, i);
i++;
} else {
printf("The city is at maximum\n");
// Maybe break here, unless you want an infinite loop
}
}
}
This is the code :
#include <stdio.h>
int funk1()
{
int N=0;
float a;
FILE *f;
f=fopen("wyniki.txt", "r");
while(fscanf(f,"%f", &a)!=EOF)
{
N++;
}
fclose(f);
return N;
}
void funk2(int N, float T[][N])
{
FILE *f;
f=fopen("wyniki.txt", "r");
float a;
int i=0;
while(fscanf(f,"%f", &a)!=EOF)
{
T[i][0]=a;
T[0][i]=a;
printf("funk 2\n");
printf("liczba = %f i = %d \n", a, i);
printf("%f %f\n", T[i][0], T[0][i]);
i++;
}
fclose(f);
}
main()
{
int N = funk1();
float T[N][N];
funk2(N,T[][N]);
int i=0;
int j=0;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
printf("|%f|\t",T[i][j]);
}
printf("\n");
printf("|%f|\t", T[i][j]);
}
getch();
return 0;
}
The problem is only with funk2. I want to pass the matrix (T) from main to funk2, do stuff with it and then give it back to main. I have no idea how to do that to be honest, I just tried out messing with it but it doesn't work. Any idea how to do that? Should I provide more information or is this enough? You can ignore the first function and the first half of funk2 as well. It all works just fine, the problem I have only has to do with passing the twodimensional array.
The result of this code is an error in
funk2(N,T[][N]);
(expected expression before ']' token).
Adding an N there results in
expected 'float (*)[(sizetype)(N)]' but argument is of type 'float'
The only time [] is valid syntax is when declaring an array either with an initializer or as a function parameter.
If you want to pass an array to a function, just pass it:
funk2(N,T);
You don't need to call out the dimensions of the array. The compiler knows it's an array.
I try to type simple 2D array of string take strings from user and print it
void in(char* n[3][2]);
void show_name ()
{
char* n[3][2];
in(n);
unsigned int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
printf("%s ", &n[i][j]);
printf("\n");
}
}
int main(void)
{
show_name();
return 0;
}
void in(char* n[3][2])
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<2;j++)
scanf("%s",&n[i][j]);
printf("\n");
}
it works correctly but i have warning say:
warning: format '%s' expects argument of type 'char*', but argument 2
has type 'char**' [-Wformat]|
I searched for reason i found that the problem in %s doesn't need for address
when i remove & operator there`s no warning but code doesn't run correctly
I have simplified and corrected your program, commenting where I changed it.
EDIT how to make it easy to change the number of strings, now 6 as above comments
#include <stdio.h>
#define STRINGS 6
void in(char n[][200]); // no need for * and array
void show_name ()
{
char n[STRINGS][200] = {}; // allow some string space and init for security
int i;
in(n);
for(i=0; i<STRINGS; i++) // only need to iterate one dimension
printf("%s\n", n[i]);
}
int main(void)
{
show_name();
return 0;
}
void in(char n[][200]) // needs length of each row, but not number of rows
{
int i;
for(i=0; i<STRINGS; i++) // only need to iterate one dimension
scanf("%199s", n[i]); // restrict the input length
printf("\n");
}
Though we declare a function with an integer array, we pass address of the array to the function. In the case of simple integers it gives error if we pass address we get pointer conversion error. But how its possible in case of an array
#include<stdio.h>
void print_array(int array[][100],int x, int y);
main()
{
int i,j,arr[100][100];
printf("Enter the array");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&arr[i][j]);
}
}
print_array(arr,i,j);
}
void print_array(int array[][100],int x,int y)
{
int i,j;
printf("\nThe values are\n");
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
printf("%d",array[i][j]);
}
}
}
My question is even though our function is declared as one with integer array as first parameter (here) we are passing array address when we call the function. How does it function?
Your are passing the array, not its address.
arr is an int[][] array
(in fact it is pretty the same as &(arr[0]), which is a pointer to (the address of) the first line of your array. In C, there is no practical difference between an array and the corresponding pointer, except you take it's address with the & operator.)
Edit: Ok, just to make me clear:
#include <stdio.h>
int fn(char p1 [][100], char (*p2)[100])
{
if (sizeof(p1)!=sizeof(p2))
printf("I'm failed. %i <> %i\n",sizeof(p1),sizeof(p2));
else
printf("Feeling lucky. %i == %i\n",sizeof(p1),sizeof(p2));
}
int main()
{
char arr[5][100];
char (*p)[100]=&(arr[0]);
fn(arr, arr);
fn(p, p);
return 0;
}
Hii ,
I have written the following code to improve it for the higher datastructures .
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int display(int *a , int *b , int *c)
{
a[0] = b;
a[1] = c;
printf("\n%d %d",a[0],a[1]); ------- point 1
printf("\n %d %d",*(a[0]),*(a[1])); ------- point 2
return 1;
}
int main()
{
int *a[5];
int b,c;
scanf("%d %d",&b,&c);
printf("%d %d",b,c);
display(a,&b,&c);
getchar();
}
I get the addresses in point 1 , but i dont get the values in point 2....What have i done wrong ... If my program itself is wrong , please jus give me a sample code that can dereference an array of pointers to get the value pointed by the element of array...
This code shouldn't compile. The signature for display should be int display(int **a , int *b , int *c), because a is a pointer to int* (remember that arrays degrade to pointers). Then, you need to write printf("\n%d %d",*a[0],*a[1]) to dereference the pointers in the array.
#include <stdio.h>
int display(int** a, int* b, int* c)
{
// store the value of b and c on array a
a[0] = b;
a[1] = c;
//print the memory addresses stored in hex format
printf("0x%x 0x%x\n", (int)a[0], (int)a[1]);
//print the values
printf("%d %d\n", *a[0], *a[1]);
return 1;
}
int main()
{
int* a[5];
int b,c;
scanf("%d %d",&b,&c);
printf("%d %d\n",b,c);
display(a,&b,&c);
getchar();
return 0;
}
The signature of display() indicates that a is a pointer. In theory, this might work in C, but gcc gave me an error. What you want to tell the compiler is that you want an array of pointers. I accomplished this with int **a in the function signature. The code below shows how I did this. Also, I cleaned it up a bit, since some of your includes aren't needed, the pointers should be printed as unsigned, display would probably be better as a void, having the "\n" at the beginning of the line was irritating in that the last line of output ended up on my prompt line, and the getchar() serves no real purpose here.
#include<stdio.h>
void display(int **a , int *b , int *c)
{
a[0] = b;
a[1] = c;
printf("%u %u\n", a[0], a[1]);
printf("%d %d\n", *a[0], *a[1]);
}
int main(void)
{
int *a[5];
int b,c;
printf("Enter two integers: ");
scanf("%d %d",&b,&c);
printf("%d %d\n",b,c);
display(a,&b,&c);
}
You want store addresses (pointers) in the vector a. Define your function to accept a vector of pointers:
int display(int *a , int *b , int *c)
This has the advantage that the compiler compiles the code.
Or better: use names that help to remember what you mean.