void check_vertical_win(char A[rows][columns]){
int i,j;
char *str1=A[i][j];
char *str2=A[i-1][j];
char *str3=A[i-2][j];
char *str4=A[i-3][j];
int elegxos1;
int elegxos2;
elegxos1=strcmp(str1,str2);
elegxos2=strcmp(str3,str4);
for(j=0;j<6;j++){
for(i=6;i>=3;i--){
if(elegxos1==0 && elegxos2==0){
printf("\nBill is the winner.\n");
}
}
}
}
I want to check if two strings in a matrix are equal,so i'm using the function strcmp. However the compiler gives warning: Initialization makes pointer from integer without a cast.
(Then when i run the programme it says: Windows are checking for a solution at my problem).
I've tried a lot to find my mistake looking other similar programmes but i can't find out.
void check_vertical_win(char A[rows][columns])
This takes a matrix of chars. If you have a matrix where each entry is a string, it should be:
void check_vertical_win(char* A[rows][columns])
Also, you're using variables i and j before they are initialized (they contain garbage):
int i,j;
char *str1=A[i][j];
char *str2=A[i-1][j];
char *str3=A[i-2][j];
char *str4=A[i-3][j];
Related
I am having problems accessing a matrix and vector with variable size using malloc() realloc() functions in a subalgorithm. I have tried the following but it doesn't seem to work.
int main()
{
char nombre[50];
sprintf(nombre,"data.txt");
int **red;
int *links;
int i, j, colSize;
links = (int*)malloc(Nred*sizeof(int));
red = (int**)malloc(Nred*sizeof(int*));
for(i=0; i<Nred; i++)
{
red[i]=(int*)malloc(1*sizeof(int));
}
LeeRed(*red, *links, Nred, nombre, &colSize);
}
void LeeRed (int ***mat, int **links, int nNodes, char *nombre, int *colSize)
{
int i, j, maxSize, nodo1, nodo2;
FILE *f, *g;
f=fopen(nombre,"rt");
g=fopen("matrizPrueba.txt", "w");
maxSize=0;
/// Number of links per node starts at 0
for(i=0; i<nNodes; i++)
{
*links[i]=0;
}
//...
}
First things first, * is the dereference operator, not the create reference operator. When you pass your matrix and links to your function, you should use & instead. For example:
LeeRed(&red, &links, Nred, nombre, &colSize);
This will make all of your types match. I would be shocked if your compiler didn't warn you about this (if it's not, try recompiling with -Wall). That being said, you really shouldn't be passing a pointer to your matrix unless you plan on modifying the pointer to your matrix. You might want to do this if you were planning on reallocating the matrix for some reason, but in the function above just make your life easier and pass the pointers by value. So change your function signature to:
void LeeRed (int **mat, int *links, int nNodes, const char *nombre, int *colSize);
Also, when you access an array with a pointer, you do not need to dereference it first, so you can use links[i] instead of *links[i]. And be careful with your allocations, right now you have an Nredx1 matrix allocated, but if you reallocate any of those columns without freeing them, you'll have a memory leak on your hands. Be sure to free everything you allocate (from the bottom up, calling free(red) will not free the columns, you must do those individually).
I'm also not sure what the links variable is intended to do, but it may be redundant if you're just trying to read into a matrix. I can't help you any further without more context.
The types of the first two parameters don't match what you're passing in.
red has type int ** and links has type int *, and you're passing *red and *links which have types int * and int respectively. This differs from the int *** and int ** types that your function is expecting for these parameters.
You're also calling the function before it's been defined or declared, so it has an implicit declaration of int LeeRead() which doesn't match the actual definition.
You should pass these two parameters directly without dereferencing:
LeeRed(red, links, Nred, nombre, &colSize);
Change the parameter types to match:
void LeeRed (int **mat, int *links, int nNodes, char *nombre, int *colSize)
Change how these two parameters are used in the function accordingly, and move the function's definition to above main.
I recently started writing chunks of C code as part of my university's programming lessons (so you can freely assume that I am a complete idiot). I'm trying to write a function that writes a 2D array's data to a file, but I'm having difficulties. I declare the array in main, I have its x and y dimensions saved as #defines, and I want to call my function() like so;
include "function.h"
#define /* x_res, y_res */
int main(){
static unsigned char pic[x_res][y_res];
/* do some operations on pic*/
function(pic,x_res,y_res);
}
The function itself is saved in a header file and is intended to be included at the very top of my main .c file. It goes something like this;
void function(unsigned char arry,int x_res,int y_res){
/* some calculations, declaring file pointer with fopen() */
for(int i=0;i<y_res;i++){
for(int j=0;j<x_res;j++){
fprintf(f,"%c",arry[i][j]);
}
}
}
I'm greeted with an error in the line fprintf(f,"%c",arry[i][j]); saying that the "subscripted value is neither array nor pointer nor vector", which is false since I know that arry is an array. Furthermore, if I try to replace said line with something like fprintf(f,"%c",arry[i*j+j]);, the error goes away, but the file output is gibberish (I'm assuming I'm only printing the addresses of the first-dimension elements of arry).
The question, then; Why can't 2D arrays be accessed like their 1D counterparts, and how do I work around this? I would imagine that an int array[][]={{0,1},{2,3}}; would give an output of
array[0] -> 0
array[1] -> 1
array[2] -> 2
array[3] -> 3
, but this is not the case -- it prints 0, 2, and then two memory addresses.
I've tried declaring my function to accept arguments as void function(unsigned char arry[*value of x_res*][*value of y_res*],x_res,y_res), which works but is not how I would like the function to work.
I've looked at some other online examples but it seems few people have had a similar problem. I tried some answers from this question but again things do not work. For example, using void function(unsigned char **arry,x_res,y_res) works with accessing the array as 2D (arry[i][j]), but again, like with the example above, most values (all that aren't in the first column) are trash.
In C99 and later, it is possible to have a VLA
void function(int x_res, int y_res, int char[][y_res])
{
for(int i=0;i<x_res;i++)
{
for(int j=0;j<y_res;j++)
{
fprintf(f,"%c",arry[i][j]);
}
}
}
The problem is that support of an implementation for VLAs was made optional in C11 (i.e. a C11 compiler is not required to support them). And VLAs are definitely not supported in C90 (the ISO C standard of 1990).
An declared array is contiguous in memory, so can be treated like a flat 1D array. For example;
void function2(int x_res, int y_res, unsigned char *arr)
{
for(int i=0;i<x_res;i++)
{
for(int j=0;j<y_res;j++)
{
fprintf(f,"%c",arr[i*y_res + j]);
}
}
}
int main()
{
unsigned char x[10][20];
unsigned char y[10*20];
unsigned char *z = malloc(10*20*sizeof(*z));
/* initialise elements x, y, and z */
function2(10,20, (unsigned char *)x);
function2(10,20, &x[0][0]);
function2(10,20, y);
function2(10,20, z);
}
The type conversion in the first call of function() is needed since a 2D array of unsigned char cannot be implicitly converted to a unsigned char *. However, the address of x and the address of x[0][0] have the same value, even though they have different types.
A gotcha with this technique is that the dimensions passed (first two arguments of function2()) are not checked at compile time. For example;
int xx[5][6];
function2(10, 20, (unsigned char *)xx); /* danger, Will Robinson!! */
function2(10, 20, &xx[0][0]); /* danger, danger!! */
will compile but, since the dimensions of xx are less than the first two arguments tell function2() to expect, will cause function2() to have undefined behaviour for both calls.
The program crashes right in the instruction mentioned in the source code (I didn't write all the code because it's too long)
int main()
{
char screen[24][80];
//......every thing is well until this instruction
backgrounds(5,screen);
//......the program doesn't execute the rest of the code
}
//______________________________________________________
//this is a header file
void backgrounds(int choice,char **screen)
{
if(choice==5)
{
screen[18][18]='-';
screen[18][19]='-';
screen[18][20]='-';
}
}
A char [24][80] cannot be converted to a char **.
When passed to a function, an array decays into a pointer to its first element. This is simple for a 1 dimensional array, but less so for higher dimensions.
In this case, a char [24][80] is an array of char [80]. So passing a variable of this type to a function yields a char (*)[80].
Change your function definition to either this:
void backgrounds(int choice,char (*screen)[80])
Or this:
void backgrounds(int choice,char screen[24][80])
Or you can use a variable length array for maximum flexibility:
void backgrounds(int choice, int x, int y, char screen[x][y])
I'm working on my assignment for my C course, and I'm trying to take in the user's input and store it in a variable to use for later in my code. Here's what my main function looks like,
int main() {
// Variables here
char* inputLine[10];
do {
printf("Insert number....");
scanf("%s\n", inputLine);
// More stuff here
}
return 0;
}
This code gives me a bunch of warnings, warning: format specifies type 'char *' but the argument has type 'char **' [-Wformat], and if I change the variable declaration to,
char* inputLine = NULL;
When I execute my code I get a seg fault, can someone explain to me what I am doing wrong, and the differences of what happens in the memory when I'm initializing this variable?
char* inputLine[10];
--> is an array of ten pointers to char
printf's format %s expects argument of type char *, but you're providing it as type char **
Just use
char inputLine[10];
To avoid possible buffer overflow you should use
scanf("%9s", inputLine); //Notice the size with %s
9 only because C string are null terminated ('\0') so one extra byte for it goes at end
char inputLine[10];
do {
printf("Insert number....");
scanf("%9s\n", inputLine);
// More stuff here
} while( //some condition);
However if you edit your code and remove * you get answer, but normal array deprecated, nowdays, programmers use vector, normal array in C not safe :
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> inputLine;
You can define with every data type:
vector<int> myvar;
Or you can define multidimensional vector:
vector< vector <int> > myvar;
I just started to look at C, coming from a java background. I'm having a difficult time wrapping my head around pointers. In theory I feel like I get it but as soon as I try to use them or follow a program that's using them I get lost pretty quickly. I was trying to follow a string concat exercise but it wasnt working so I stripped it down to some basic pointer practice. It complies with a warning conflicting types for strcat function and when I run it, crashes completly.
Thanks for any help
#include <stdio.h>
#include <stdlib.h>
/* strcat: concatenate t to end of s; s must be big enough */
void strcat(char *string, char *attach);
int main(){
char one[10]="test";
char two[10]="co";
char *s;
char *t;
s=one;
t=two;
strcat(s,t);
}
void strcat(char *s, char *t) {
printf("%s",*s);
}
Your printf() should look like this:
printf("%s",s);
The asterisk is unnecessary. The %s format argument means that the argument should be a char*, which is what s is. Prefixing s with * does an extra invalid indirection.
You get the warning about conflicting types because strchr is a standard library routine, which should have this signature:
char * strcat ( char * destination, const char * source );
Yours has a different return type. You should probably rename yours to mystrchr or something else to avoid the conflict with the standard library (you may get linker errors if you use the same name).
Change
printf("%s",*s);
to
printf("%s",s);
The reason for this is printf is expecting a replacement for %s to be a pointer. It will dereference it internally to get the value.
Since you declared s as a char pointer (char *s), the type of s in your function will be just that, a pointer to a char. So you can just pass that pointer directly into printf.
In C, when you dereference a pointer, you get the value pointed to by the pointer. In this case, you get the first character pointed to by s. The correct usage should be:
printf( "%s", s );
BTW, strcat is a standard function that returns a pointer to a character array. Why make your own?
Replacing *s with s won't append strings yet, here is fully working code :
Pay attention to function urstrcat
#include <stdio.h>
#include <stdlib.h>
/* urstrcat: concatenate t to end of s; s must be big enough */
void urstrcat(char *string, char *attach);
int main(){
char one[10]="test";
char two[10]="co";
char *s;
char *t;
s=one;
t=two;
urstrcat(s,t);
return 0;
}
void urstrcat(char *s, char *t) {
printf("%s%s",s,t);
}
pointers are variable which points to address of a variable.
#include "stdio.h"
void main(){
int a,*b;
a=10;
b=&a;
printf("%d",b);
}
in the follwing code you will see a int 'a' and a pointer 'b'.
here b is taken as pointer of an integer and declared by giving'' before it.'' declare that 'b' is an pointer.then you will see "b=&a".this means b is taking address of integer "a" which is keeping value 10 in that particular memory and printf is printing that value.