Why there is an error? [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I wrote the following code block I have all the time error in the function find_brackets and calculation. can someone explain to me how to fix it. And the two functions will function together
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void find_brackets(char str[], int len);
void calculation(char str1);
int main(void) {
int len;
char str1[99];
char str[99]; // (4/2)
printf("Enter a math exercises: \n");
gets(str);
len = strlen(str);
find_brackets(str);
calculation(str1);
}
void find_brackets(str[], len) {
char str1[len];
int i, j;
for(i = 0; i < len; i++) {
if(str[i] == '(') {
i++;
while(str[i] != ')') {
str1[j] = str[i];
i++;
j++;
}
}
}
}
void calculation(str1[], len) {
char str[len];
char strp[len];
char str2[len];
char str3[len];
char *rev;
int i, k, j = 0, aPos, zPos;
int sum1, sum2;
float sum;
strcpy (str, str1);
strcpy (strp, str1);
aPos = zPos = -1;
for(i = 0; i < len; i++) {
if(str[i] == '+') {
aPos = i;
}
else if(str[i] == '/') {
zPos = i;
break;
}
}
if(aPos != -1 && zPos != -1) {
for(k = 0, i = zPos-1; i > aPos; --i, ++k) {
str2[k] = str[i];
}
}
rev = strrev(str2);
printf("%s\n", rev);
for(i = 0; i < len; i++) {
if(strp[i] == '/') {
while(strp[i+1] != '+') {
str3[j++] = strp[++i];
}
}
}
printf("%s\n", str2);
sum1 = atoi(str2);
sum2 = atoi(str3);
sum = sum1 / sum2;
printf("%.0f\n", sum);
}
Thanks for the help I appreciate it

Function declaration is void find_brackets(char str[], int len); and the caller from main() is find_brackets(str); which is wrong. Where is the 2nd arg.
Also function calculation() has differnce in declaration and how it is invoked. Maintain a match in function formal arguments followed by callee actual arguments passed.

void find_brackets(str[],len)
void calculation(str1[],len)
please specify data type of len and str[] at function definations.
Also
find_brackets(str);
calculation(str1);
which pass one argument but you declared that with two which is also wrong.

Related

Detect the shortest word in a sentence in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have created the below program to detect the shortest word in a sentence for me. However, the result is not what I expected. I went through the code a few times and I still could not find the problem.
I would be very grateful if someone could lend me some help.
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
ssize_t find_short(const char *s);
int main(void)
{
char s[100] ="lets talk about C the best language";
printf("%zu", find_short(s));
}
ssize_t find_short(const char *s)
{
int n = strlen(s);
int smallest = n;
int counter = 0;
for (int i = 0; i <= n; i++)
{
if (s[i] == ' ' || s[i] == '\0')
{
if (counter <= smallest)
{
smallest = (ssize_t) counter;
counter = 0;
}
}
else
counter ++;
}
return smallest;
}
Thank you very much.
You reset counter only when counter is smallest than smallest. If a new word is shorter than any previous, it will be ignored.
ssize_t find_short(const char *s)
{
int n = strlen(s);
int smallest = n;
int counter = 0;
for (int i = 0; i <= n; i++)
{
if (s[i] == ' ' || s[i] == '\0')
{
if (counter <= smallest)
{
smallest = (ssize_t) counter;
// <-- not here
}
counter = 0; // < -- here
}
else
counter ++;
}
return smallest;
}
counter=0 should be outside the if statement
ssize_t find_short(const char *s)
{
int n = strlen(s);
int smallest = n;
int counter = 0;
for (int i = 0; i <= n; i++)
{
if (s[i] == ' ' || s[i] == '\0')
{
if (counter <= smallest)
{
smallest = (ssize_t) counter;
}
counter = 0;
}
else
counter ++;
}
return smallest;
}

My program can't seem to separate words at certain cases [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am mostly concerned with the find and separation function. When I reused them in another program, I found that they don't always work and sometimes show segmentation fault. I tried every possible thing I could do, but nothing worked.
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
int find(char *argument)
{
int count = 0;
int i = 0;
int state = 0;
while (argument[i] != '\0')
{
if (argument[i] == ' ' || argument[i + 1] == '\0')
{
state = 0;
}
else if (state == 0)
{
state = 1;
count++;
}
i++;
}
return count;
}
char **sepration(int args, char str[])
{
int i = 0;
char **argv = (char **)malloc(args);
int k = 0;
int len = strlen(str);
while (i < len)
{
if (str[i] != ' ')
{
char *st = &str[i];
argv[k] = st;
k++;
while (str[i] != ' ' && str[i] != '\0' && i < len)
{
i++;
}
str[i] = '\0';
}
i++;
}
return argv;
}
int main()
{
char argument[100];
scanf("%[^\n]s", &argument);
//finds the no of words in the given argument
int args = find(argument);
char **argv = (char **)malloc(args + 1);
//stores the word separately in **char pointer array
argv = sepration(args, argument);
//adds the arguments whichs are numbers
add(args, argv);
}
The problem with your solution is that you are not allocating the memory properly.
Your double pointer argv should work as an array of strings. Hence argv should have enough memory allocated for the same. Then next question is how to allocate sufficient memory for the same. As argv should hold argc number of strings hence proper memory allocation will be char **argv = malloc(args * sizeof(char *)); or,
char *argv[argc];
Modified code will look like
char **sepration(int args, char str[])
{
int i = 0;
char **argv = malloc(args * sizeof(char *));
int k = 0;
int len = strlen(str);
while (i < len)
{
if (str[i] != ' ')
{
char *st = &str[i];
argv[k] = st;
k++;
while (str[i] != ' ' && str[i] != '\0' && i < len)
{
i++;
}
str[i] = '\0';
}
i++;
}
return argv;
}
int main()
{
char argument[100];
scanf("%[^\n]", argument);
//finds the no of words in the given argument
int args = find(argument);
char **argv = malloc(args * sizeof(char *));
//stores the word separately in **char pointer array
argv = sepration(args, argument);
for(int i = 0; i < args; i++)
printf("%s\n", argv[i]);
}
Instead of char **argv = malloc(args * sizeof(char *)); you can use char *argv[argc]; as well. This will also give the same result.

How to scan 2-dimensional char array in c [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to get string(including white spaces)input save to 2-dimensional array.
my code is this :
char a[10000][6];
scanf("%d", &n);
for (int i = 0;i < n;i++)
{
scanf("%[^\n]s", a[i]);
}
for (int i = 0;i < n;i++)
{
printf("%s\n", a[i]);
}
What I got that is wrong output.Please give me any suggestion!
Use
char a[10000][6];
scanf("%d",&n);
int i;
for (i = 0;i < n;i++)
{
scanf("%s",&a[i]);
}
I recently solved this question in my program. I came to the conclusion that the best way to read input from keyboard is to handle character one by one. You can use code below.:
bool in()
{
int i;
int a;
int len;
int max;
char *text[10000];
//change 'max' somehow here before loop
for(i = 0, len = 0; i < max; i++)
{
text[i] = NULL;
do
{
do
{
a = getchar();
}
while(a == '\n' && !text[i]);
if(text[i])
{
len = strlen(text[i]);
len++;
}
else
{
len = 1;
}
text[i] = realloc(text[i], sizeof(char)*(len+1));
if(!text[i])
{
printf("cant realloc\n");
return false;
}
if(a != '\n')
{
text[i][len-1] = (char) a;
text[i][len] = '\0';
}
else
{
text[i][len-1] = '\0';
}
}
while(a != '\n');
}
return true;
}

Unknown error regarding strings and stdio.h library [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm receiving an uknown error found in the stdio.h library.
Please can someone check it at tell me what is wrong with the code (But I thing it should work fine).
P.S. I'm new here so please don't blame me if this is a bad question.
Code:
#include <stdio.h>
#include <stdlib.h>
// Conversion from a number to a string
char *i2s(int broj);
int main()
{
char string1;
int br, n;
do
{
printf("How much numbers?\n -"), scanf("%d", &n);
} while (n < 1);
for (int i = 0; i < n; i++)
{
printf("\nbr = "), scanf("%d", &br);
string1 = *i2s(br);
printf(" %s", string1);
}
free(string1);
getch();
return 0;
}
char *i2s(int broj)
{
char *pom;
int z=0,br=0,p;
if (broj < 0)
{
z = 1;
broj = -broj;
}
p = broj;
do
{
br++;
p /= 10;
} while (p);
pom = (char *)calloc(br + 1 + z, sizeof(char));
if (z)
pom[0] = '-';
do
{
pom[--br + z] = '0' + broj % 10;
} while (broj /= 10);
return pom;
}
char string1;
free(string1);
string 1 is not a pointer.
Also with the following section you overwrite string1 everytime you run through the loop. that way you have no pointer to free() the memory that you allocate inside your function unless you do it inside the loop.
for (int i = 0; i < n; i++)
{
printf("\nbr = "), scanf("%d", &br);
string1 = *i2s(br);
printf(" %s", string1);
}

undefined reference when compiling in gcc [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
ok so i have this code that is supposed to read text from a file and do some computation which is not too important for this question. When I try to compile it I get this error(file name is pp3.c)
/tmp/ccnZaQld.o:pp3.c:(.text+0x5f): undefined refrence to 'sovle'
/tmp/ccnZaQld.o:pp3.c:(.text+0x5f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol 'solve'
collect2: error: ld returned 1 exit status
here is my code
#include <stdio.h>
#define SIZE 100
char buff[SIZE][SIZE];
int initialize();
int read(char*);
int findstart();
int sovle(int,int,int,int);
int printmaze();
int posx, posy, oldposy, oldposx;
int main(int argc, char* argv[]){
initialize();
read(argv[1]);
findstart();
if(sovle(posx, posy,oldposy,oldposx))
printmaze();
else
printf("maze is unsolveable");
}
int initialize(){
int i;
int m;
for(i=0; i<SIZE; i++){
for(m=0; i<SIZE; i++){
buff[i][m]='0';
}
}
}
int read(char* m88){
FILE* myfile = fopen(m88, "r");
int linenum = 0;
while(fgets(buff[linenum], SIZE, myfile)!=NULL){
linenum++;
}
fclose(myfile);
}
int findstart(){
int i,m;
for(i=0; i<SIZE; i++){
for(m=0; i<SIZE; i++){
if (buff[i][m]== 'S'){
posx = m;
posy = i;
oldposy = i;
oldposx = m;
return 1;
}
}
}
}
int solve(int x, int y, int oldx, int oldy){
if(buff[y][x+1] != 'x' && x+1 != oldposx){
if(buff[y][x+1] == '$')return 1;
if(solve(y,x+1,y,x)){
buff[y][x+1] = '*';
return 1;
}
}
if(buff[y][x-1] != 'x' && x-1 != oldposx){
if(buff[y][x-1] == '$')return 1;
if(solve(y,x-1,y,x)){
buff[y][x-1] = '*';
return 1;
}
}
if(buff[y+1][x] != 'x' && y+1 != oldposy){
if(buff[y+1][x] == '$')return 1;
if(solve(y+1,x,y,x)){
buff[y+1][x] = '*';
return 1;
}
}
if(buff[y-1][x] != 'x' && y-1 != oldposy){
if(buff[y-1][x] == '$')return 1;
if(solve(y-1,x,y,x)){
buff[y-1][x] = '*';
return 1;
}
}
}
int printmaze(){
int i;
while(buff[i][0]!= '0'){
printf("%s", buff[i]);
i++;
}
}
You have a typo in your source. sovle is not solve:
The one it's complaining about is here:
if(sovle(posx, posy,oldposy,oldposx))
printmaze();
It doesn't help that you've declared the typo as being a function:
int findstart();
int sovle(int,int,int,int);
int printmaze();

Resources