LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2023, 06:23 PM   #1
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 365

Rep: Reputation: Disabled
Implicit declaration of function_strncmp


Hello again,
this is some sort of continuation of a thread I marked as solved a few days ago.
Asking other people and searching the web, i put up together a few more lines to help me inputting some co-ordnates on terminal to locate a word placed in a grid.

Sadly, and contrarily to what i was suggested in the other thread, I am still using an IDE, (KDevelop) as I find it useful for a newbie to locate errors much quicker and at least visualize their location/s.
Now, I came across something really peculiar.

I found out that "_strncmp" is a windows-specific-function whereas "strncasecmp" is POSIX compliant.
Both give me a compile error and the program doesn't execute (it does on VSCode).

But the peculiar thing is that the same function is called in other lines and KDevelop does NOT give an error (I attached a scrnshot to better visualize my query).
Would anyone be so kind to explain why, in a language that a 6yr old could understand?
Thanks
Attached Thumbnails
Click image for larger version

Name:	scrnshot.png
Views:	10
Size:	66.3 KB
ID:	40268  
 
Old 01-17-2023, 07:47 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 23,913

Rep: Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268
I don't use VScode but there is a c function called strncmp

Code:
int res;
res=strncmp(c1, "exit",4);
if (res==0)  // strings match.
...
//for case insensitive.  Since the string needs to match exactly so the \n is added.
res=strcasecmp(c1,"exit\n");
if ( res==0) 
...
I would keep the play loop in main but I am not writing the program...

Last edited by michaelk; 01-17-2023 at 07:56 PM.
 
Old 01-17-2023, 09:46 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,145

Rep: Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672Reputation: 2672
I don't use an IDE, but it's possible KDevelop only 'Warns' on the first occurrence ?

Incidentally most modern editors eg vim have syntax checkers built in / add-ons, so you should get that kind of warning I think.
 
Old 01-17-2023, 10:55 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,683

Rep: Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008
Quote:
Originally Posted by however View Post
I found out that "_strncmp" is a windows-specific-function whereas "strncasecmp" is POSIX compliant.
Both give me a compile error and the program doesn't execute (it does on VSCode).
Do you have #include <strings.h>? You need that for strncasecmp.

https://manned.org/strncasecmp.3
 
1 members found this post helpful.
Old 01-17-2023, 11:44 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,428
Blog Entries: 1

Rep: Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679
Add this to the beginning of the program:
Code:
#ifdef _Windows
  #define strcasecmp _stricmp
  #define strncasecmp _strnicmp
#else
  #define _stricmp strcasecmp
  #define _strnicmp strncasecmp
#endif
 
1 members found this post helpful.
Old 01-18-2023, 04:51 AM   #6
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 365

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
I don't use VScode but there is a c function called strncmp

I would keep the play loop in main but I am not writing the program...
It ended being a typo in the function
Code:
if (strncmp(c1, "EXIT\n", 4) == 0 || strincmp(c2, "EXIT\n", 4) == 0)
Quote:
Originally Posted by ntubski
Do you have #include <strings.h>? You need that for strncasecmp.
Yes, I have #include <strings.h>

Quote:
Originally Posted by chrism01
I don't use an IDE, but it's possible KDevelop only 'Warns' on the first occurrence ?

Incidentally most modern editors eg vim have syntax checkers built in / add-ons, so you should get that kind of warning I think.
It wasn't just a warning, I believe; it wouldn't let me execute the program at all.

Quote:
Originally Posted by NevemTeve
Add this to the beginning of the program:
Code:

#ifdef _Windows
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#else
#define _stricmp strcasecmp
#define _strnicmp strncasecmp
#endif
Wow! thank you. That looks like a sneaky trick that I would have discovered in months. I guess, by "beginning of the program" you mean with the bunch of "#include<>" stuff, correct?

Last edited by however; 01-18-2023 at 04:53 AM.
 
1 members found this post helpful.
Old 01-18-2023, 05:18 AM   #7
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 365

Original Poster
Rep: Reputation: Disabled
I feel that i am very close to the end of this.

The following lines (which altogether, ended up being more than 300 and not the 100ish as I was hoping for initially) should the job as I tested the playGame function and it works finding the coordinate of a word. However, when i put it in the whole program it does not behave as it did earlier when i tested it alone.

Could anyone tell me where I have messed things up somewhere?

Code:
// Changing coordinate values to integers
int getIntValue(char x)
    {
	return x - '0';
    }

int getCharIntValue(char x)
    {
	return toupper(x) - 'A';
    }

//Check if the word correct
int checkIfWin(char wordFound[], int win)
{
	int i;

	for (i = 0; i < 4; i++)
	{
		if (strncmp(wordFound, fourWords[i], strlen(wordFound)) == 0)
		{
			win++;
		}
	}
	return win;
}

void playGame()
{
	char c1[5], c2[5];
	char wordFound[10] = " ";
	int i, j, k, l, m, d, x, win = 0;

	do
	{
		printf("\nEnter first coordinate and press Enter: ");
		fgets(c1, 5, stdin);

		if (strncmp(c1, "EXIT\n", 4) == 0 || strncmp(c2, "EXIT\n", 4) == 0)
		{
		//	WordSearch();
		}

		printf("Enter last coordinate and press Enter: ");
		fgets(c2, 5, stdin);

		if (strncmp(c1, "EXIT\n", 4) == 0 || strncmp(c2, "EXIT\n", 4) == 0)
		{
		//	WordSearch();
		}

		if (c1[1] == c2[1])
		{
			// Horizzontal Word
			d = 0;
			memset(wordFound, 0, sizeof(wordFound));

			for (i = getCharIntValue(c1[0]); i <= getCharIntValue(c2[0]); i++)
			{
				wordFound[d] = puzzle[getIntValue(c1[1])][i];
				d++;
			}

			printf("Word Found is '%s'", wordFound);

			win = checkIfWin(wordFound, win);
			printf("\nWords found are: %d\n", win);
		}
		else if (getCharIntValue(c1[0]) == getCharIntValue(c2[0]))
		{
			// Vertical Word
			d = 0;
			memset(wordFound, 0, sizeof(wordFound));

			for (i = getIntValue(c1[1]); i <= getIntValue(c2[1]); i++)
			{
				wordFound[d] = puzzle[i][getCharIntValue(c1[0])];
				d++;
			}

			printf("Word Found is '%s'", wordFound);

			win = checkIfWin(wordFound, win);
			printf("\nWords found are: %d\n", win);
		}
		else
		{
			j = getCharIntValue(c1[0]);
			k = getIntValue(c1[1]);
			l = getCharIntValue(c2[0]);
			m = getIntValue(c2[1]);
			d = 0;
			x = 0;

			memset(wordFound, 0, sizeof(wordFound));

			for (i = j; i <= l; i++)
			{
				wordFound[d] = puzzle[k + x][j + x];
				d++;
				x++;
			}

			printf("Word Found is '%s'",wordFound);

			win = checkIfWin(wordFound, win);
			printf("\nAmount of words found is: %d\n", win);
		}
	}
	while (win != 4);
	printf("\nThe End!\n");
	return ;
}

//Create a user-interactive menu in normal/console colours
void mainMenu()
{
    printf("\033[0m");
    char menuChoice;
    do
    {
        printf("\n~~~ DAILY CROSSWORD ~~~\n");
        printf("1. Play\n");
        printf("3. Exit\n");
        printf("\nWhat would you like to do: \n");

        while((menuChoice = getchar()) == '\n');

        switch (menuChoice)
        {
            case '1': displayPuzzle();
                printf("\n------------------------");
                printf("\nUse coordinate to solve\nthe puzzle; i.e. C3 G3\n");
                printf("------------------------\n");
                //printf("Enter your co-ordinate\n");
                playGame();
                break;
        }

    } while (menuChoice != '3');
}

int main(int argc, char *argv[])
{
    srand(time(NULL));

    createBlankPuzzle();
    displayPuzzle();
    fillPuzzleWithWords();
    displayWords();

    mainMenu();
    getchar();
    getFourRandomWords();

    printf("Thank you for playing today! :)\nGood Bye\n");

    return 0;
}
Thank you

To be completely honest, I did not write all of the above (maybe only 25% to adapt it to Linux compiler and change some whistles&bells) but it seems to work OK on VSCode
 
1 members found this post helpful.
Old 01-18-2023, 11:25 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 23,913

Rep: Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268
I guess you need to clear the buffer.
Code:
          case '1': displayPuzzle();
                printf("\n------------------------");
                printf("\nUse coordinate to solve\nthe puzzle; i.e. C3 G3\n");
                printf("------------------------\n");
                //printf("Enter your co-ordinate\n");
                while ((getchar()) != '\n');  //clear buffer
                playGame();
                break;
 
1 members found this post helpful.
Old 01-18-2023, 11:48 AM   #9
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 365

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
I guess you need to clear the buffer.
Code:
          case '1': displayPuzzle();
                printf("\n------------------------");
                printf("\nUse coordinate to solve\nthe puzzle; i.e. C3 G3\n");
                printf("------------------------\n");
                //printf("Enter your co-ordinate\n");
                while ((getchar()) != '\n');  //clear buffer
                playGame();
                break;
Awesome! thanks a lot that worked like a charm (although I discovered more bugs thanks to it).

On a different note, I can start to see the power of coding, especially in C or C++ but I wish i had started earlier. The learning curve is steeper than climbing the Everest.

When I fix one thing, there are another 2 broken.

Thanks everyone.

edit: ignore my comment. It works nicely now. I was entering the wrong commands (Coord.1 and coord. 2 [enter] instead of coord.1[enter] then coord.2[enter]. Proud of my(ish) very 1st C program

Last edited by however; 01-18-2023 at 11:58 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
warning: implicit declaration of function liguorir Linux - Software 5 10-22-2012 04:20 PM
Implicit declaration means alaios Programming 7 03-09-2009 07:20 AM
warning: incompatible implicit declaration of built-in function ‘exit’ xzotech Programming 1 08-14-2005 06:43 AM
implicit declaration of function 'getdelim' MicahCarrick Programming 2 05-02-2005 12:03 PM
gcc 3.4.1 and c implicit declaration foo_bar_foo Programming 1 01-27-2005 05:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration