The strncmp function is a string comparison function found in languages like C and C++. In contrast with strcmp, the strncmp function takes a third argument denoting the maximum length of the strings. This is used to prevent out of bounds errors.
Questions tagged [strncmp]
85 questions
1
vote
1 answer
Comparing a single string to an array of strings in C
My program is accepting user input and then taking the first word inputted and comparing it to an array of accepted commands. What would be the best way to compare the first word inputted (after it has been tokenized) to an array of…

IrateIrish
- 219
- 2
- 6
- 12
1
vote
3 answers
passing argument 1 of ‘strncmp’ makes pointer from integer without a cast C programing
I want to compare some data that the client sends to a server. Bout i have this error when I use strncmp t compare what I read from the socket and the strings I'm using to compare. Can someone tell me why is this error?
warning: passing argument 1…

netfreak
- 63
- 2
- 4
- 9
1
vote
3 answers
C Strncmp Return Partial Input
I currently have a linked list structure running and I need to find a way to let a user search the struct for a certain field. I have this done, but the problem is it must be exact. For example, if the user enters "maggie" it will return the result,…

James Manes
- 526
- 1
- 11
- 23
1
vote
8 answers
how to compare character against set of given characters in C?
I'd like to be able to compare a character on stdin with a characters of my specification. The purpose of this is to filter out every other input as wrong, while maintaining only the specified single chars as commands. Like on stdin "nn" or "qddaw"…

Markus
- 686
- 1
- 11
- 18
0
votes
1 answer
Ignoring whitespaces while reading input files in C
I'm trying to write code that registers the first word of every line as a command, but I want to be able to read the word regardless of there being spaces or not in front of it. I'm currently using fgets() and strncmp the first x characters of each…

John
- 11
- 1
- 3
0
votes
6 answers
strncmp function does not stop checking at n characters?
My program compares the 2 strings entirely and does not stop once n number of characters are reached? Why does this happen?
int strncompare (const char* mystring1,const char* mystring2, int number)
{
int z;
z = number - 1;
while…
0
votes
1 answer
Why is my strcmp always returning true?
I'm trying to compare user text input from an iphone app with the text in a static array I have declared. It is always returning "true", even when the text is different. After doing the strncmp, I display both text fields. To the human eye, they…

Rick
- 49
- 13
0
votes
1 answer
Segmentation Fault 11 , I know which part of code is wrong, but IDK why
my gcc version is 12.2.0 (Homebrew GCC 12.2.0)
While running the code, the terminal reads segmentation fault 11.
By using print method and breakpoint method, I narrow the problem down to where it went wrong.
However, I couldn't see why.
where it…

New Leaf
- 11
0
votes
2 answers
How could I compare a "substring" of a character array with another character array?
I am writing a program where I need to check if an object matches a certain type. The way it's written so far, you need to compare the character-array of the object with the character-array of the category you are looking for.
I am using strcmp to…

psychosys
- 57
- 1
- 6
0
votes
1 answer
strncmp() Clang-Tidy: Comparison length too long and might lead to buffer overflow
I am iterating over my command line arguments and checking for matches with
if (!strncmp(argv[i], "-f", 3)) {}
Clang-Tidy warns me that the 3 is too large comparison length and might lead to a buffer overflow. In my understanding, that should be…

ZwergofPhoenix
- 141
- 1
- 6
0
votes
2 answers
Where is strnicmp on Windows?
I'm trying to compile Python on Windows in an unusual context, and running into this problem:
a.o : error LNK2019: unresolved external symbol strnicmp referenced in function connection_clear
Okay, so I'm not linking in the proper library containing…

rwallace
- 31,405
- 40
- 123
- 242
0
votes
1 answer
string.h functions - strncpy and strncat strange behaviour
I was testing some of the string.h function and found a strange behaviour when using the functions strncpy and strncat.
#include
#include
int main()
{
char name1[30], name2[30], name3[30], name4[30], name5[30];
char…

Dalton Cézane
- 3,672
- 2
- 35
- 60
0
votes
1 answer
Complexity of strncmp in string.h
I would like to know the time complexity of int strncmp(const char *__s1, const char *__s2, size_t __n) which is in the C library(string.h).
I have to study the complexity of my whole program which is calling strncmp thousands of time, I can't…

Zero
- 17
- 4
0
votes
3 answers
Strcmp doesn't work and I don't seem to understand why - transformation to ASCII code makes the program work as expected though
First of all, please don't critique the way the program is written, because this is what we study in my country.
I know it is a mixture of C and C++ and the things I use are outdated, but that's how the things are here.
So I have to make a program…

Octavian Niculescu
- 423
- 4
- 10
0
votes
0 answers
creating compare string function
I created a function that compares strings, and as I was frustrated about it always missing the last character in the second string and always returning "identical strings" as a result, I noticed that I was messing around and used gets() instead of…

arthur_kyle
- 1
- 1