简体   繁体   English

C-使用'=='运算符,两个指针相等是什么意思?

[英]C - What does it mean that both pointers are equal using '==' operator?

I'm a person in somewhere who is studying C language 我是一个正在学习C语言的人

I'm just curious of some parts of '==' operator. 我只是对'=='运算符的某些部分感到好奇。

I do know that pointer is a variable which stores address of memory. 我确实知道指针是一个存储内存地址的变量。 But here comes a question. 但是这里有一个问题。 When I tried to use '==' operator , eventhough those two pointers are pointing different address , the '==' operator worked. 当我尝试使用'=='运算符时,尽管那两个指针指向不同的地址,但'=='运算符仍然有效。

Here's my code below 这是我的下面的代码

I want to know why the statement r == s considered to be True and why r and s gives me some 'non-sense value' which must be 90 ( in my computer those values are 56 )? 我想知道为什么语句r == s被认为是True,以及为什么r和s给我一些“无意义的值”,该值必须为90(在我的计算机中,这些值为56)?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
void add(int* , int* , int*);
int* Add(int* , int* , int*);
int main(){
    int a[N] , b[N] , c[N];
    int m ;
    int* p;
    int* q;
    int* r;
    int* s;
    char* ptr1 = "ATGC";
    char* ptr2 = "TCGA";
    char* ptr3 = ptr1;

    printf("result from strncmp : %d\n",strncmp(ptr1,ptr2,4));
    if(strncmp(ptr1,ptr2,4)==0){
        printf("strncmp operator works! those two are same\n");
    }else{
        printf("strncmp operator do not work! those two are not same\n");
    }
    printf("Okay another example here\n");
    if(ptr1 == ptr2){
        printf("ptr1 and ptr2 are same\n");
    }else if(ptr1==ptr3){
        printf("ptr1 and ptr3 are same");
    }else{
        printf("?\n");
    }
    printf("Now compare values after function call\n");
    for(m = 0 ; m < N ; m++){
        a[m] = m;
        b[m] = m*m;
        c[m] = 0;
    }
    printf("Works as void function\n");
    add(a,b,c);
    printf("After function call\n");
    for(m = 0 ; m < N ; m++){
        printf("%d + %d = %d\n",a[m],b[m],c[m]);
    }
    p = &c[N-1];
    q = &c[N-1];
    r = Add(a,b,c);
    s = Add(a,b,c);

    if(p==q){
        printf("Those two pointers p and q are same\n");
        printf("Address of pointer p is 0x%p\n",&p);
        printf("Address of pointer q is 0x%p\n",&q);
        printf("Value of pointer p is %d\n",*p);
        printf("Value of pointer q is %d\n",*q);
    }else{
        printf("Those are differenct\n");
    }

    if(r==s){
        printf("Those two pointers r and s are same\n");
        printf("Address of pointer r is 0x%p\n",&r);
        printf("Address of pointer s is 0x%p\n",&s);
        printf("Value of pointer r is %d\n",r);
        printf("Value of pointer s is %d\n",s);
    }else{
        printf("Those are differenct\n");
    }
    return 0;
}

void add(int* a , int* b , int* c){
    int i ;
    for(i = 0 ; i < N ; i++){
        c[i] = a[i] + b[i];
        printf("inside of  function loop : %d , current c is %d + %d = %d\n",i,a[i],b[i],c[i]);
    }
}

int* Add(int* a , int* b , int* c){
    int i ;
    for(i = 0 ; i < N ; i++){
        c[i] = a[i] + b[i];
        printf("inside of  function loop : %d , current c is %d + %d = %d\n",i,a[i],b[i],c[i]);
    }
}

You are not returning anything from Add function. 您没有从Add函数返回任何内容。 But you are using its return value, this is undefined behavior. 但是您正在使用它的返回值,这是未定义的行为。

You got r==s because both the times Add implicitly returned same arbitrary/garbage value. 之所以得到r==s是因为两次Add隐式返回相同的任意/垃圾值。

Basically returning a value from function is same as popping out some value from the function's stack memory (and sometimes saving it in CPU Reg R0 ). 从函数返回值基本上与从函数的堆栈存储器中弹出某些值相同(有时将其保存在CPU Reg R0 )。 When you do not specify which value to return, an arbitrary chosen value by compiler will be popped out and used. 不指定要返回的值时,将弹出并使用编译器选择的任意值。

To avoid such mistakes, always enable -Wall option (atleast) of your ( gcc ) compiler, and pay attention to every warning. 为避免此类错误,请始终启用( gcc )编译器的-Wall选项(至少),并注意所有警告。

Because you printed a pointer, and address of this pointer but not the value it's pointing at. 因为您打印了一个指针,所以该指针的地址而不是它指向的值。

Use *r and *s , if you want to print what it pointing at (holds). 如果要打印指向(保留)的内容,请使用* r和* s。

printf("Those two pointers r and s are same\n");
printf("Address of pointer r is 0x%p\n",r);
printf("Address of pointer s is 0x%p\n",s);
printf("Value of pointer r is %d\n",*r);
printf("Value of pointer s is %d\n",*s);

or 要么

printf("Value of pointer r is %d\n",r[0]);
printf("Value of pointer s is %d\n",s[0]);
printf("Value of pointer r is %d\n",r[1]);
printf("Value of pointer s is %d\n",s[1]);
printf("Value of pointer r is %d\n",r[2]);
printf("Value of pointer s is %d\n",s[2]);
printf("Value of pointer r is %d\n",r[3]);
printf("Value of pointer s is %d\n",s[3]); 

.... ....

The function int* Add(int* a , int* b , int* c) is not returning any address to caller. 函数int* Add(int* a , int* b , int* c)不会向调用方返回任何地址。 The function Add can return global or heap memory to the caller. 函数Add可以将全局或堆内存返回给调用方。 That is returning the stack(local variable) address to the caller still leads a various kind of problem. 那就是将栈(本地变量)地址返回给调用者仍然导致各种问题。

You are calling the Add function, but you are not returning anything from the function. 您正在调用Add函数,但没有从该函数返回任何内容。 so it result undefined behavior. 因此会导致未定义的行为。

Try the following change- 尝试以下更改-

int* Add(int* a , int* b , int* c){
     int i ;
     for(i = 0 ; i < N ; i++){
         c[i] = a[i] + b[i];
         printf("inside of  function loop : %d , current c is %d + %d =    %d\n",i,a[i],b[i],c[i]);
     }
     return c; // Fix
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM