2016-02-15 133 views
0

我竭力要了解這部分的意思是:什麼!s || !*的意思? (S是char *)

if (!s || !*s) //What is this?!?! 
     { 
     printf("\n"); 
     return; 
     } 

這是我的主要功能:

#include <conio.h> 
#include <stdio.h> 
#include <string.h> 

void func1(char *s); 

int main() 
{ 
    func1("SABABA"); 
    return 0; 
} 

而且我FUNC1:

void func1(char *s) 
{ 
    int a, b, c; 
    if (!s || !*s) 
    { 
     printf("\n"); 
     return; 
    } 
    b = strlen(s); 
    while (b>2) 
    { 
     a = 0; 
     if (s[a] == s[b - 1]) 
     { 
      for (c = b - 1; c >= a && s[a] == s[c]; a++, c--); 
      if (c<a) 
      { 
       int i; 
       for (i = 0; i<b; i++) 
        printf("%c", s[i]); 
       printf(" "); 
      } 
     } 
     b--; 
    } 
    func1(s + 1); 
} 

我的意見:'s'表示字符串的地址,而!s表示當我們在字符串的「堆棧」之外。例如如果起始地址是1000並且字符串是6個字符,則我們以1006結束。如果我們超過1006,例如,到1007那麼它就會返回true。和* s,它檢查地址1000保存的值,即「S」,這是真的,意思是!* s是錯誤的。並且因爲我們知道每個字符串都以「/ 0」結尾,所以我想在1007處搜索那個。如果我是對的,那麼爲什麼我們需要兩個!s和* s,爲什麼不只是其中之一。我甚至得到這個對嗎?

+3

在入門書上C.閱讀上的指針' !s'檢查's'不是空指針,'!* s'解引用's'並檢查它不指向NUL字符。 –

+0

這些是可怕的變量和函數名稱! :D – mikeyq6

+0

@ mikeyq6它來自測試。它只有7點,以確定此功能和它的輸出。我認爲他們在1997年寫了它。 –

回答

8

!s正在檢查s不是空指針。

!*s正在檢查字符s指向不是'\0',這意味着s不是空字符串。

該檢查與調用func1("SABABA")不匹配,但是準備用於諸如func1(0)func1("")

+0

爲什麼!有必要?這不是一個dync。分配。這意味着「SABABA」將被分配給s,這將顯然具有記憶。或者如果RAM滿了,它會返回一個空指針? –

+1

@IlanAizelmanWS:你爲什麼認爲它不應該?假設's'爲空,如果你試圖遵守它會發生什麼?僅僅因爲你專門用非空字符串調用函數,並不意味着它總是會這樣,你不應該假設它會。 –

+3

@IlanAizelmanWS'func1()'不知道如何調用它,會傳遞什麼,不是嗎?想想'func1(0)'和'func1(「」)'。 – songyuanyao

1

!不是(=否定者)。 任何非零值假定爲真,0爲假。 so:

void *ptr = NULL; 
void *ptr2 = "444"; // ptr2 will point to a real address, which is never NULL (=0) 

if(ptr) {} // This evaluates to false, because 'ptr' points to 0 
if(!ptr) {} // ptr is 0 => false, but theres still a negator, so the result 

if(ptr2) {} // true 
if(!ptr2) {} // false 

結果將是真實的。

* ptr指的是ptr指向的值。 這樣:

int x = 5; 
int *ptr; 
ptr = &x; // ptr points to a memory cell, which is filled with an integer with value 5 

if(*ptr) {} // is true, because the value pointed by ptr is a non-zero value. 
if(!*ptr) {} // negate previous result => false 

SOOOO:(!小號|| * S) 如果 在的話,這意味着:( 'S' 是一個指向字符串的指針,或者也許NULL) 執行,如果體如果:

小號== NULL

OR

第一字符(值)由 'S' 指出是0(= \ 0)

在這些情況下,該測試爲真:

s = NULL; 
s = "\0 This string starts with an 0 character"; 
s = ""; // empty string, but "" implicitly appends a '\0' 
+0

因此,如果(!ptr){}將返回false? –

+0

在第二個例子中,如果(* ptr){}'是未定義行爲,那麼您正在訪問您不擁有的內存,並且您無法知道它是否爲'0'。 – mch

+0

哦,是的,sry,我的意思是這樣的:int * ptr; int x = 5; ptr =&x; – Jim

0

聲明if (!s || !*s)檢查是否指針s是空指針或者如果指針的目標是需要'\0'。前者檢查,因爲你可以從malloc。這裏未來的指針提供此功能是一個例子:

#include <conio.h> 
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

void func1(char *s); 

int main() 
{ 
    char *str = "SABABA"; 
    char *ptr = (char*)malloc(10 * sizeof(char)); 

    func1(str); 

    //ptr may have null value,that's why we check in func1 
    func1(ptr); 

    free(ptr); 
    return 0; 
} 
+0

「ptr可能有空值,這就是爲什麼我們檢查func1」是不正確的。 'func1'只會檢查'* ptr',不包括其他9個字符。 – songyuanyao

相關問題