2014-05-12 120 views
0

我試圖編寫一個函數打印文本在C爲PIC微控制器(我認爲它是基於gcc)。函數與字符數組參數

void print(char[] text); 

void print(char[] text) 
{ 
    for(ch = 0; ch < sizeof(text); ch ++) 
    { 
     do_something_with_character(text[ch]); 
    } 
} 

,並調用它像這樣:

print("some text"); 

我收到關於錯誤的括號編譯器的投訴。

  1. 這是什麼問題?

  2. 如何在這種情況下使用char數組指針?變量名後

+0

可能的重複[如何在C++中使用數組?](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – Barracuda

+0

實際上'char [] text '不是有效的C語法。它應該是'char text []'或'char * text' –

回答

2

我會做這樣的:

void print(char *text); 

void print(char *text) 
    { 
    while(*text) 
     { 
     do_something_with_character(*text); 
     ++text; 
     } 
    } 
+5

等等......人們*實際上*使用那種縮進風格?我的眼睛...... –

+0

@JonathonReinhart,大部分是老年人,來自「舊學校」。 –

1

通行證C風格串由指針,或括號:

void print(char *text); 
       ^

void print(char text[]); 
        ^^ 

爲了計算一個字符串的長度,使用strlensizeof不。

int len = strlen(text); 
for(ch = 0; ch < len; ch ++) 
       ^^^^ 
+0

請注意,在循環條件中使用'strlen()'會將線性算法轉換爲二次算法,因爲每次迭代都會計算長度,除非編譯器真的很聰明並可以確定沒有任何更改字符串的長度。 –

+0

@JonathanLeffler:好點,編輯。我來自C++領域,它在循環中調用'string :: length()'並不差。 – deepmax

4

正如在其他的答案中提到,你的語法不正確。在text之後括號屬於

同樣的顯著重要性,sizeof做你期待什麼在這裏:

void print(char[] text) 
{ 
    for(ch = 0; ch < sizeof(text); ch ++) 
        ^^^^^^ 

記住,sizeof編譯時運營商 - 編譯器與大小,而替換它代碼正在構建中。它在運行時不可能知道大小。

在這種情況下,在大多數32位系統上,sizeof(text)總是要返回sizeof(void*)或4。你的平臺可能有所不同


你有兩個選擇:

  • 轉至另一個參數打印長度。
  • char[]視爲"C string",其中長度未知,但字符串由NUL字符終止。

後者是你最好的選擇,你應該用char*代替char[]

在這裏我們使用一個NULL結尾的字符串,並遍歷它的指針:

void print(const char* text) 
{ 
    const char* p; 

    // Iterate over each character of `text`, 
    // until we reach the NUL terminator 
    for (p = text; *p != '\0'; p++) { 

     // Pass each character to some function 
     do_something_with_character(*p); 
    } 
} 
+0

如何動態測量char數組的大小? – Kamil

+0

@Kamil請參閱['strlen'](http://pubs.opengroup.org/onlinepubs/009695399/functions/strlen.html)。但是請注意:在處理典型的C字符串時,您很少關心字符串的實際長度。 –

+0

我需要迭代該文本... – Kamil

2

你必須把方括號在正確的位置:

void print(char[] text); 

void print(char[] text) 

void print(char text[]); 

void print(char text[]) 

或使用指針符號:

void print(char *text); 

void print(char *text) 

另請注意,即使你使用數組符號,參數被有效改寫爲指針,然後在函數體代碼:

for(ch = 0; ch < sizeof(text); ch ++) 

是錯誤的。你幾乎肯定不需要4或8個字節,它是指針的大小。您可能想要:

size_t len = strlen(text); 
for (size_t ch = 0; ch < len; ch++) 

如果您不能在循環中聲明變量(C99或更高版本要求),請另行聲明它。您沒有在代碼中顯示ch的聲明。如果編譯,這意味着ch是一個全局變量 - 這是非常可怕的。

注意,++運營商緊密結合,不應該從它的操作用空格隔開。

+0

留下描述性解釋有什麼意義?只要給我一個採樣碼。 –

+0

'這是世界之路。儘管從大多數觀點來看,這是沒有道理的。 –

3

正確的語法是

void print(char text[]) 

void print(char *text) 

print(),則不能使用sizeof運營商,查找字符串text的長度,你需要使用strlen()(包括<string.h>第一)或測試text[ch]是否爲\0

1

看你的問題,你可能是在C初學者的觀察,我會嘗試回答你的問題,而無需使用指針像這裏其他的答案。

首先,喬納森·萊因哈特使一個很好的點,即sizeof是不正確的用法在這裏。此外,由於有人指出,對於字符數組,這也是您使用的是你的代碼是什麼,正確的語法如下:

// empty character array 
// cannot be changed or given a value 
char text[]; 

// character array initialized to the length of its content 
char text[] = "This is a string"; 

// character array with length 1000 
// the elements may be individually manipulated using indices 
char text[1000]; 

在你的情況,我會做這樣的事情:

#include <string.h> 

void print(char text[]); 

int main() 
{ 
    char text[] = "This is a string"; 
    print(text); 

    return 0 
} 

void print(char text[]) 
{ 
    // ALWAYS define a type for your variables 
    int ch, len = strlen(text); 

    for(ch = 0; ch < len; ch++) { 
     do_something_with_character(text[ch]); 
    } 
} 

標準庫頭string.h提供strlen函數返回不包括終止空字符\0字符串的長度的一個整數值(實際上無符號長整數)。在C中,字符串只是字符數組,並且指定字符串結尾的方式是在末尾包含\0