I am having one function which is returning character pointer and it prototype is shown below.
我有一个函数返回字符指针,它的原型如下所示。
char *substring(char *string, int position, int length);
calling function
char *temp1;
temp1 = substring(de1Binary, FieldNo - 1, 1);
return pointer if it is printed with %s it is showing temp1 = 1 After this the below code is executed
返回指针如果用%s打印它显示temp1 = 1在此之后执行以下代码
if (temp1 == "1")
{
Inside loop
}
But it is not going inside loop.Here I think I am missing some basic concept in C program. Please guide me.
但它不会进入循环内部。我认为我在C程序中缺少一些基本概念。请指导我。
2 个解决方案
#1
2
If you want to compare the two strings use the strcmp() function it is in string.h header file.
如果要比较两个字符串,请使用strcmp()函数,它位于string.h头文件中。
Instead of if(temp1 == "1") you can use the below if statement.
而不是if(temp1 ==“1”),您可以使用下面的if语句。
if((strcmp(temp1, "1")) == 0)
{
Inside loop
}
#2
1
The problem is that if you even write
问题是,如果你甚至写
if ("1" == "1")
there is no guarantee that this condition will yield true
. It depends on the compiler options. It can store each of identical string literals separatly and in this case they will have different addresses or store only one string literal of identical literals.
无法保证这种情况会产生真实。这取决于编译器选项。它可以分别存储每个相同的字符串文字,在这种情况下,它们将具有不同的地址或仅存储一个字符串文字相同的文字。
String literals have type of character arrays. In the if-condition string literals are converted implicitly to pointers to their first characters. So in this statement
字符串文字具有字符数组的类型。在if-condition中,字符串文字被隐式转换为指向其第一个字符的指针。所以在这个声明中
if (temp1 == "1")
there is compared the address stored in temp1 with the address of the first character of string literal "1" that is stored in memory like
将temp1中存储的地址与存储在内存中的字符串文字“1”的第一个字符的地址进行比较
{ '1', '\0' }
So this condition will always yield false
or either true or false (depending on the compiler options) if you write before the if statement
因此,如果在if语句之前写入,则此条件将始终产生false或true或false(取决于编译器选项)
temp1 = "1";
You need to compare the strings pointed to by these two pointers not the pointers themselves. You could do this for example the following way
你需要比较这两个指针指向的字符串而不是指针本身。您可以通过以下方式执行此操作
if ( temp1[0] == "1"[0] && temp1[1] == '\0' ) {*...*/ }
or
if ( temp1[0] == '1' && temp1[1] == '\0' ) {*...*/ }
Imagine for example that you have a manifest constant
想象一下,例如你有一个清单常量
#define One "1"
In this case you could write indeed
在这种情况下,你可以写
if ( temp1[0] == One[0] && temp1[1] == '\0' ) {*...*/ }
But it is much better to use standard C function strcmp
declared in header <string.h>
that returns 0 if two strings are equal.
但是使用在header
if ( strcmp( temp1, "1" ) == 0 ) { /*...*/ }
#1
2
If you want to compare the two strings use the strcmp() function it is in string.h header file.
如果要比较两个字符串,请使用strcmp()函数,它位于string.h头文件中。
Instead of if(temp1 == "1") you can use the below if statement.
而不是if(temp1 ==“1”),您可以使用下面的if语句。
if((strcmp(temp1, "1")) == 0)
{
Inside loop
}
#2
1
The problem is that if you even write
问题是,如果你甚至写
if ("1" == "1")
there is no guarantee that this condition will yield true
. It depends on the compiler options. It can store each of identical string literals separatly and in this case they will have different addresses or store only one string literal of identical literals.
无法保证这种情况会产生真实。这取决于编译器选项。它可以分别存储每个相同的字符串文字,在这种情况下,它们将具有不同的地址或仅存储一个字符串文字相同的文字。
String literals have type of character arrays. In the if-condition string literals are converted implicitly to pointers to their first characters. So in this statement
字符串文字具有字符数组的类型。在if-condition中,字符串文字被隐式转换为指向其第一个字符的指针。所以在这个声明中
if (temp1 == "1")
there is compared the address stored in temp1 with the address of the first character of string literal "1" that is stored in memory like
将temp1中存储的地址与存储在内存中的字符串文字“1”的第一个字符的地址进行比较
{ '1', '\0' }
So this condition will always yield false
or either true or false (depending on the compiler options) if you write before the if statement
因此,如果在if语句之前写入,则此条件将始终产生false或true或false(取决于编译器选项)
temp1 = "1";
You need to compare the strings pointed to by these two pointers not the pointers themselves. You could do this for example the following way
你需要比较这两个指针指向的字符串而不是指针本身。您可以通过以下方式执行此操作
if ( temp1[0] == "1"[0] && temp1[1] == '\0' ) {*...*/ }
or
if ( temp1[0] == '1' && temp1[1] == '\0' ) {*...*/ }
Imagine for example that you have a manifest constant
想象一下,例如你有一个清单常量
#define One "1"
In this case you could write indeed
在这种情况下,你可以写
if ( temp1[0] == One[0] && temp1[1] == '\0' ) {*...*/ }
But it is much better to use standard C function strcmp
declared in header <string.h>
that returns 0 if two strings are equal.
但是使用在header
if ( strcmp( temp1, "1" ) == 0 ) { /*...*/ }