When calling a function, why do I get these errors? - c

I get these warnings that I believe are preventing anything from printing:
"passing argument 1 of 'printboard' makes pointer from integer without a cast" when I call the function and "expected char(*)[26] but argument is of type 'char'" on line 2.
#include <stdio.h>
void printBoard(char board[26][26], int n){
int i,j,k;
if (i=0){
for (j=1;j<n;j++){
for (k=1;k<n;k++){
board[i][j]=k;
}
printf("%c", k+96);
}
}
if (j=0){
for(i=1;i<n;i++){
for(k=1;k<n;j++){
board[i][j]=k;
printf("%c", k+96);
}
}
}
for (i=1;i<=n;i++){
for (j=1;j<=n;j++){
if (board[i][j]!=board[n/2][n/2]&&board[i][j]!=board[(n/2)+1][(n/2)+1]&&board[i][j]!=board[n/2][(n/2)+1]&&board[i][j]!=board[(n/2)+1][n/2]){
board[i][j]='U';
}
else if (board[i][j]==board[n/2][n/2]&&board[i][j]==board[(n/2)+1][(n/2)+1]){
board[i][j]='W';
}
else {
board[i][j]='B';
}
}
printf("%c", board[i][j]);
}
//n is even
return;
}
int main(void)
{
const char board[26][26];
int dim;
char boardConfig;
printf("Enter the board dimension: \n");
scanf("%d", &dim);
printBoard(board[26][26],dim);

board[26][26] is a char element (out of bounds). You want to pass the entire 2D array board:
printBoard(board,dim);
(also: check return code of scanf, and check if dim is within 1-26 bounds to avoid undefined behaviour)
And when initializing board[i][j]=k; add some ASCII offset (ex: board[i][j]=k+'a';). Otherwise, your code will print non-ASCII characters at the end.

You need to typecast it to the parameter type. There are a number of errors in your code - such as the assignment operators instead of comparison operators. if(i=0) should be if(i==0), etc. I got the code compiled and working, not certain as to what you are trying to do yet, but this should work:
#include <stdio.h>
void printBoard(char board[26][26], int n){
int i = 0;
int j = 0;
int k = 0;
if (i==0){
for (j=1;j<n;j++){
for (k=0;k<n;k++){
board[i][j]=k;
}
printf("%c", k+96);
}
}
if (j==0){
for(i=1;i<n;i++){
for(k=0;k<n;k++){
board[i][j]=k;
printf("%c", k+96);
}
}
}
for (i=1;i<=n;i++){
for (j=1;j<=n;j++){
if (board[i][j]!=board[n/2][n/2]&&board[i][j]!=board[(n/2)+1][(n/2)+1]&&board[i][j]!=board[n/2][(n/2)+1]&&board[i][j]!=board[(n/2)+1][n/2]){
board[i][j]='U';
}
else if (board[i][j]==board[n/2][n/2]&&board[i][j]==board[(n/2)+1][(n/2)+1]){
board[i][j]='W';
}
else {
board[i][j]='B';
}
}
printf("%c", board[i][j]);
}
//n is even
return;
}
int main(void)
{
const char board[26][26];
int dim;
char boardConfig;
printf("Enter the board dimension: \n");
scanf("%d", &dim);
printBoard((char (*)[26])board,dim);
}

Related

My c program keeps crashing and I do not know why

I am writing a program like hangman in c and I am trying to run it .The problem is that it's working fine until I give it a letter to quess the word but then it crashes with -1073741819 (0xC0000005). Can someone help me solve this, I think its something really small that I cant
see . Thank you for helping me!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Rules(void);
void maskWord (char starword[], int size);
int playRound(char starword[], char answer[]);
void updateStarWord(char starword[], char answer[], char userguess);
int occurancesInWord(char userguess, char answer[]);
int c=0;
char answer[128];
int N;
int main()
{
int ch,size;
char starword[1000];
do
{
printf("---Welcome to Hangman!---\n");
printf("1.Start Game\n2.Instructions\n3.Exit\n");
scanf("%d",&ch);
if(ch>0&& ch<4)
{
switch(ch)
{
case 1:
maskWord(starword,size);break;
case 2:
Rules();break;
}
}
else
system("cls");
}
while(ch!=3);
return 0;
}
void Rules(void)
{
do
{
do
{
printf("\nThe word to guess is represented by a row of dashes representing each letter of the word.");
printf("\nRules may permit or forbid proper nouns, such as names, places, brands, or slang.");
printf("\nIf the guessing player suggests a letter which occurs in the word, the other player writes it in all its correct positions.");
printf("\nIf the suggested letter does not occur in the word, the other player draws one element of a hanged stick figure as a tally mark.\n");
}
while(getchar()!='\n');
}
while(getchar()!='\n');
}
void maskWord (char starword[], int size)
{
printf("Enter word to guess: ");
fflush(stdout);
scanf(" %s", answer);
int N = strlen(answer);
int mask[N];
for (int i=0; i < N; ++i)
{
mask[i] = 0;
}
playRound(mask,N);
}
int playRound(char starword[], char answer[])
{
// Loop over each round of guessing
int gameover = 0;
while (! gameover)
{
// Print word with *s for unguessed letters
printf("The word is : ");
for(int j=0; j < answer; ++j)
{
if (starword[j])
{
printf("%c", answer[j]);
}
else
{
printf("*");
}
}
printf("\n");
// Get player's next guess
char guess;
printf("\nGive a letter: ");
fflush(stdout);
scanf(" %c", &guess);
updateStarWord(starword,answer,guess);
}
}
void updateStarWord(char starword[], char answer[], char userguess)
{
// Mark true all mask positions corresponding to guess
int k;
for(k=0; k < answer; ++k)
{
if ((answer[k]) ==(userguess))
{
starword[k] = 1;
}
}
}
Your for loop doesn't make sense because the condition for terminating it is k < answer. You are comparing an integer (k) to a pointer (answer). The compiler should have warned you about this, so make sure your compiler warnings are turned on and you are paying attention to them. Pointers and integers are different things, and comparing them is almost never what you want to do.
If answer is null-terminated, you could probably replace that condition with answer[k]. Or maybe updateStarWord needs to take an argument that indicates the length of answer, and then the condition would be k < answer_length.

Problem with finding successive integers in C

The main problem of the code is that finally the programm shows every time that there are not successive integers.
At first, I tried to figure out a solution to this problem, by investigating how to correct the "if" statement and then to fix some small mistakes on the code, but I was unable to find any error. The code is below
#include <stdio.h>
int main() {
int a,i;
int A[10];
for(i=0; i<=9; i++) {
scanf("%d",&a);
A[i]=a;
}
if ((A[i+1]-A[i]==1)||(A[i+1]-A[i]==-1)) {
printf("{%d,%d}",A[i+1],A[i]);
} else {
printf("Den yparxoun diadoxikoi arithmoi");
}
return 0;
}
Well, the expected result is to show, if exist, the successive integers as pairs. For example if I write the integers 4,-1,9,8,3,5,-21,6,7,8 the program should print {9,8}{6,7}{7,8}. The actual result is to show every time that there are not successive integers.
Thank you in advance for your help.
This should do it:
#include <stdio.h>
int main() {
int a,i;
int A[10];
int c =0;
for(i=0; i<=9; i++) {
scanf("%d",&a);
A[i]=a;
}
for(i=0;i<=9;i++)
{
if(A[i+1]==10)
{
break;
}
else if ((A[i+1]-A[i]==1)||(A[i+1]-A[i]==-1))
{
printf("{%d,%d}",A[i],A[i+1]);
c=1;
}
}
if(!c)
printf("Den yparxoun diadoxikoi arithmoi");
return 0;
}
You are supposed to use a loop in order to find out the pair by adding the loop your code looks like this
#include <stdio.h>
int main()
{
int a,i,flag=0;
int A[10];
for(i=0; i<=9; i++)
{
scanf("%d",&a);
A[i]=a;
}
for(i=0;i<9;i++){
if ((A[i+1]-A[i]==1)||(A[i+1]-A[i]==-1))
{
printf("{%d,%d}",A[i+1],A[i]);
flag=1;
}
}
if(!flag)
printf("Den yparxoun diadoxikoi arithmoi");
return 0;
}

Compare an int with double array

I'm trying to learn C and I want to make a program that compares the number I type with the numbers in my array. The only problem is that It doesn't actually do that. Even if I type a number that is from that array it shows that the number is not from that array.
#include <stdio.h>
void getMark(int findMark, double crswk1[]);
void changePartMark(double crswk1[], int findMark);
int main()
{
int findMark;
double crswk1[10]={67, 77, 80, 40};
getMark(findMark, crswk1);
changePartMark(crswk1, findMark);
}
void getMark(int findMark, double crswk1[])
{
printf("Enter the mark you want to change: ");
scanf("%d", &findMark);
}
void changePartMark(double crswk1[], int findMark)
{
int i;
if(findMark == crswk1[i])
{
printf("It is equal");
}
else
{
printf("It is not equal");
}
}
The number you're reading in is never getting back to findMark in your main function.
void getMark(int findMark, double crswk1[])
{
printf("Enter the mark you want to change: ");
scanf("%d", &findMark);
}
This function is saving a value in the local parameter findMark. Because all parameters in C are passed by value, changes to this local variable are not reflected in the caller, so findMark in main never changes.
You need to change this function to take the address of an `int
void getMark(int *findMark, double crswk1[])
{
printf("Enter the mark you want to change: ");
scanf("%d", findMark);
}
Then you call this function from main like this:
getMark(&findMark, crswk1);
By passing in the address of findMark, the function can write to that address.
Also, your changePartMark function doesn't search through the entire array. It only looks at index i. But even that is a problem because you never set i.
You need to loop through the array to check your value against each element in the array.
int i;
for (i=0; i<4 i++) {
if(findMark == crswk1[i])
{
printf("It is equal");
}
else
{
printf("It is not equal");
}
}
Two main issues:
First, the number you enter is never passed back. When you write
void getMark(int findMark, double crswk1[]) {
printf("Enter the mark you want to change: ");
scanf("%d", &findMark);
}
then you read in the value in a local copy findMark, not in the one used by the caller. BTW: crswk1 is not used; So I'd suggest to write
int getMark() {
int findMark = 0;
printf("Enter the mark you want to change: ");
scanf("%d", &findMark);
return findMark;
}
Second, your void changePartMark(double crswk1[], int findMark) lacks a loop, and i is not initialized. Code could look like the following:
void changePartMark(double crswk1[], int findMark)
{
for (int i=0; i<4; i++) {
if(findMark == crswk1[i])
{
printf("It is equal");
}
else
{
printf("It is not equal");
}
}
}

Stack around the variable was corrupted with pointer arithmetics

With the code below, I'd always run into "Stack around the variable 'UserCode' was corrupted.
If I'm not mistaken, when I do userCode = (char*)malloc(sizeof(char)*N);, shouldn't it create an "array" with size of char*n ? I'm guessing my issue is either with my declaration of an array, or my pointer arithmetic.
Any help would be highly appreciated.
#include "stdafx.h"
#include <math.h>
int userPrompt1() {
int numOfAlphabets = 0;
printf("Please enter a number from 1 to 8 to choose how many alphabets you want\n");
scanf_s(" %d", &numOfAlphabets);
if (numOfAlphabets > 8 || numOfAlphabets < 0) {
printf("Sorry! Invalid number entered. Try again. \n");
numOfAlphabets = userPrompt1();
}
return numOfAlphabets;
}
int userPrompt2() {
int numOfLetters = 0;
printf("Please enter the number of letters you want to guess\n");
scanf_s(" %d", &numOfLetters);
if (numOfLetters < 0) {
printf("Sorry! Invalid number entered. Try again. \n");
numOfLetters = userPrompt2;
}
return numOfLetters;
}
int tryCalculator(int K, int N) {
int tries = 0;
tries = 1 + ceil(N * log2(K));
return tries;
}
void codeGenerator(char codeGuessIn[], char letters[], int size) {
for (int i = 0; i < size; i++) {
int rando = rand() % size;
codeGuessIn[i] = letters[rando];
printf(" %c", codeGuessIn[i]);
}
printf("\n");
}
void codeChecker(char codeGuessIn[], char generatedCode[], int size) {
int correctAlphabets = 0;
for (int i = 0; i < size; i++) {
if (codeGuessIn[i] == generatedCode[i]) {
correctAlphabets++;
}
}
printf(" %d in correct place \n", correctAlphabets);
}
void getUserCode(int size, char *userCode[]) {
for (int i = 0; i < size; i++) {
printf("Please enter letter #%d \n", i+1);
getchar();
scanf_s(" %c", &userCode[i]);
}
}
int main(void)
{
char letters[8] = { 'A','B','C','D','E','F','G','H' };
char *generatedCode; //array to hold generated code
char *userCode; // array to hold generated code.
int K = userPrompt1(); //how many different alphabets in code
int N = userPrompt2(); //how many letters in code
int tries = tryCalculator(K, N);
//int gameEnd = 1;
userCode = (char*)malloc(sizeof(char)*N);
generatedCode = (char*)malloc(sizeof(char)*N);
codeGenerator(generatedCode, letters, N);
getUserCode(N, &userCode);
//codeChecker(userCode, generatedCode, N);
return 0;
}
void getUserCode(int size, char *userCode[]) {
scanf_s(" %c", &userCode[i]);
Here, userCode[i] is a char * (pointer-to-char), &userCode[i] is a char ** (pointer-to-pointer-to-char), and scanf("%c") expects a char *. A good compiler would warn about that.
I think what you meant to do here is something like:
void getUserCode(int size, char *userCode) {
scanf_s(" %c", &userCode[i]);
}
int main(void) {
char *userCode = malloc(N);
getUserCode(N, userCode);
}
The printf(), getchar(), scanf() combination here reeks of the bad habits created by scanf: you're discarding the first character entered by the user because you're relying on an extra character in the input buffer.
See http://c-faq.com/stdio/scanfprobs.html and read full lines of input with fgets() instead of using scanf().
Also,
int userPrompt2() {
int numOfLetters = 0;
...
numOfLetters = userPrompt2;
}
You're assigning a function pointer to an int. (A normal compiler should warn about this.) If the idea here is to call the function again to repeat the prompt in case the user enters something silly, it's probably a better idea to use a loop instead of a recursive call anyway.

Passing multidimensional arrays by void functions

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.

Resources