[操作疑難] 又係 C pointer 問題

我 pass 一個 integer address 去一個 function 冇事.
  1. #include <stdio.h>
  2. my_scanf(int *ptr_x) {
  3.         scanf("%d", ptr_x);
  4. }
  5. main() {
  6.         int x;
  7.         my_scanf(&x);
  8. }
複製代碼
但 pass 一個 string address 去一個 function 出事.
鬧 expected ‘char **’ but argument is of type ‘char (*)[100]’
  1. #include <stdio.h>
  2. my_scanf(char *ptr_x[]) {
  3.         scanf("%s", ptr_x);
  4. }
  5. main() {
  6.         char x[100] = "world";
  7.         my_scanf(&x);
  8. }
複製代碼
唔係好明, 要點改呢?

我 pass 一個 integer address 去一個 function 冇事.但 pass 一個 string address 去一個 function 出事.
...
Mojoe 發表於 2011-10-5 00:18



    但 pass 一個 string address 去一個 function 出事.
You are not passing a string address

TOP

剛剛學 C, 唔係好識. 咁應該點改呢?

TOP

string address of an array can be found by using its name
ie.
char abc[14];
it address is abc

TOP

  1. #include <stdio.h>
  2. int my_scanf(char ptr_x[]) {
  3.     return  scanf("%99s", ptr_x);
  4. }

  5. int main() {
  6.     char x[100] = "world";
  7.     my_scanf(x);
  8.     return 0;
  9. }
複製代碼

TOP

多謝! 多謝!

TOP

C does not have the "String" data type.
There is char array only

TOP