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

时间:2014-08-01 05:27:50

标签: c pointers

我是某个正在学习C语言的人

我只是对'=='运算符的某些部分感到好奇。

我知道指针是一个存储内存地址的变量。但这里有一个问题。 当我尝试使用'=='运算符时,尽管这两个指针指向不同的地址,但'=='运算符仍有效。

以下是我的代码

我想知道为什么语句r == s被认为是真的,为什么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]);
    }
}

4 个答案:

答案 0 :(得分:7)

您没有从Add函数返回任何内容。但是你正在使用它的返回值,这是未定义的行为。

你得到r==s,因为Add次都隐含地返回了相同的任意/垃圾值。

基本上从函数返回一个值与从函数的堆栈内存中弹出一些值(有时将其保存在CPU Reg R0中)相同。如果未指定要返回的值,则会弹出并使用编译器选择的任意值。

为避免此类错误,请始终启用(-Wall)编译器的gcc选项(至少),并注意每个警告。

答案 1 :(得分:1)

因为您打印了一个指针和该指针的地址,但不是它指向的值。

如果要打印指向(保持)的内容,请使用* 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);

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

...

答案 2 :(得分:0)

函数int* Add(int* a , int* b , int* c)未向调用者返回任何地址。函数Add可以将全局或堆内存返回给调用者。那就是将堆栈(局部变量)地址返回给调用者仍会导致各种问题。

答案 3 :(得分:0)

您正在调用Add函数,但您没有从函数返回任何内容。所以它导致了未定义的行为。

尝试以下更改 -

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
}