undefined reference when compiling in gcc [closed] - c

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();

Related

Function getting redeclared [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 2 years ago.
Improve this question
#include<stdio.h>
#define MAX 10000
#define CHECK 1000
#define OPN 1
#define CLS 0
char program[MAX];
void si_qoute (void);
void do_qoute (void);
void getprogram (void);
int main(){
printf ("Type your code\n");
getprogram ();
si_quote ();
do_quote ();
return 0;
}
void si_quote (void){
int opn[CHECK], cls[CHECK], i, lcnt = 0, opn_cnt = 0;
for (i = 0; i < CHECK; i++)
opn[i] = cls[i] = 0;
for (i = 0; program[i] != '\0'; i++){
if (program [i] == '\n')
lcnt++;
if (program[i] == '\''){
opn[opn_cnt] = lcnt;
if (program[i + 1] == '\\' && program[i + 3] =='\''){
cls[opn_cnt++] == lcnt;
i += 3;
}
else if(program[i + 2] == '\''){
cls[opn_cnt++] == lcnt;
i += 2;
}
else
opn_cnt++;
}
}
opn[opn_cnt] = -1;
for(i = 0; opn[i] != -1; i++)
if (opn[i] && cls[i] == 0)
printf ("Single Quote opened at line %d not closed \n", opn[i]);
}
void do_quote (void){
int opn[CHECK], cls[CHECK], i, lcnt = 0, opn_cnt = 0;
for (i = 0; i < CHECK; i++)
opn[i] = cls[i] = 0;
for (i = 0; program[i] != '\0'; i++){
if (program[i] == '\n')
lcnt++;
if (program[i] == '\"'){
opn[opn_cnt] = lcnt;
while (program[++i] != '\"'){
if (program[i] == '\\' && program [i + 1] == '\"')
i++;
else if (program[i] == '\n'){
opn_cnt++;
lcnt++;
break;
}
}
if (program[i] == '\"')
cls[opn_cnt++] = lcnt;
}
}
opn[opn_cnt] = cls[opn_cnt] = -1;
for (i = 0; opn[i] != -1; i++)
if (opn[i] && cls[i] == 0)
printf ("Double inverted quoted opened at %d not closed \n", opn[i]);
}
void getprogram (void){
int i, c;
for (i = 0; i < MAX - 1 && (c = getchar()) != EOF; i++)
program[i] = c;
program[i++] = '\n';
program[i] = '\0';
}
I am getting and error which states implicit declaration of two functions si_quote() and do_quote(). What I am intending to do is to call these functions from main. While calling a function as per my knowledge we need to try write the function name along with the argument list within parenthesis. However as these functions doesn't accept any arguments, the parameter list is empty. I think the functions are probably getting redeclared with int as their return type. I can't understand what is really the issue with it. If it is getting redeclared as per my guess why is getprogram() still working fine. I would really appreciate if someone could help me out.
You have si_quote in one place and si_qoute in the other. These are not equal.
This simple typo makes the compiler think you declare (but not use) two of the functions and use (but do not declare) two other, completely unrelated functions.

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;
}

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;
}

Why is this code getting an assignment to 'char *' from 'char' error? [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 6 years ago.
Improve this question
I am getting errors while compiling.
incompatible integer to pointer conversion assigning to 'string'
(aka 'char *') from 'char'; take the address with &
My Code:
#include<stdio.h>
#include<cs50.h>
#include<string.h>
int pallin(string A);
int main(void)
{
printf("Enter the string to analyze\n");
string S[10];
S = GetString();
int flag = pallin(S);
if(flag == 0)
{
printf("Invalid input\n");
}
else if (flag == 1)
{
printf("Yes, the input is a pallindrome\n");
}
else{
printf("The input is not a pallindrome\n");
}
}
int pallin(string A)
{
int flag;
int n = strlen(A);
if(n<=1)
{
return 0;
}
else
{string B[10];int i = 0;
while(A[i]!="\0")
{
B[i]=A[n-i-1]; //Getting error here.
i++;
}
for(int j = 0; j < n; j++)
{
if(B[j]!=A[j])
{
flag = 2;
}
else
{
flag = 1;
}
}
return flag;
}
}
I'm not fond of the CS50 typedef char *string; — it doesn't help enough and does cause far too much confusion. You can't declare arrays of characters using string.
This code works:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int palin(string A);
int main(void)
{
printf("Enter the string to analyze\n");
string S = GetString();
int flag = palin(S);
if (flag == 0)
{
printf("Invalid input\n");
}
else if (flag == 1)
{
printf("Yes, the input is a palindrome\n");
}
else
{
printf("The input is not a palindrome\n");
}
}
int palin(string A)
{
int flag;
int n = strlen(A);
if (n <= 1)
{
return 0;
}
else
{
char B[100];
int i = 0;
//while (A[i] != "\0")
while (A[i] != '\0')
{
B[i] = A[n - i - 1]; // Getting error here.
i++;
}
for (int j = 0; j < n; j++)
{
if (B[j] != A[j])
{
flag = 2;
}
else
{
flag = 1;
}
}
return flag;
}
}
Changes are to string S = GetString(); in main(); char B[100]; in palin(); respelled 'palindrome'; use '\0' in place of "\0" (which has other problems too; it's the same as "" in this context, and that isn't how you compare strings (in the generic sense as well as the CS50 sense) — you need strcmp() if you want to compare strings, but you don't in this context).
It doesn't free the allocated string. It does produce the correct answers (program name pa19):
$ pa19
Enter the string to analyze
amanaplanacanalpanama
Yes, the input is a palindrome
$ pa19
Enter the string to analyze
abcde
The input is not a palindrome
$ pa19
Enter the string to analyze
Invalid input
$

Why there is an error? [closed]

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.

Resources