『壹』 c語言字元串在函數間傳遞
#include<stdio.h>#include<string.h>char *start(char *wz);int main(){ char *sys = NULL; char xz,wz[99]="www"; scanf("%s",&xz); if (xz=='1') sys=start(wz);/*將wz值傳入start*/ printf("%s",sys);
if (sys != NULL) // 注意:分配內存以後一定要釋放
free(sys); return 0;}char *start(char *wz){
char* str = (char*)malloc(99); // 堆中分配內存
strcpy(str, "am start -a android.intent.action.VIEW -d http://"); strcat(str,wz); return str; }
其實不建議以這種方式來寫,start函數可以寫成2元函數,一個函數傳入參數,一個函數傳出結果。
void start(char* pOut, char* pIn)
{
strcpy(pOut, "am start -a android.intent.action.VIEW -d http://");strcat(pOut, pIn);
}
『貳』 C語言:編寫一個函數,由實參傳來一個字元串,統計此字元串中字母,數字,空格和其他字元的個數。
char *ch,就是定義一個字元型的指針,來接收指針,在你的程序里就是接收你輸入的字元串的首地址。要想返回實參,根據你的目的,應該是各類字元的個數,可以用一個數組實現。
int* sum(char *ch,int sum[4]);用一個長度為4的一維數組來統計各類字元的個數;
同樣int* 表示返回一個整形指針;
你應該這樣調用sum()函數;p=sum(ch,sum);(當然你之前要,在main()里定義一個整形指針接受sum()的返回值),把sum[4]={0}初始化全部為0,),用p[0],p[1],p[2],p[3]表示
字母,數字,空格和其他字元的個數。
不明白的再問我
『叄』 c語言中 如果給函數形參傳遞字元串
單個字元用單引號引起來,比如'\0'你都用了雙引號,改過來就好了
『肆』 C語言如何在兩個函數之間傳送字元串
用指針,例如:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void abc(char *str){
strcpy(str,"string from abc\0");
};
void def(char *str){
printf("print in def: %s\n",str);
};
main()
{
char str[30];
abc(&str[0]);
def(&str[0]);
exit(0);
}
// abc()中給值,def()印出。
『伍』 c語言:字元串做為函數參數傳遞
1、值傳遞
void swap(int x,int y)
{ int temp = x;
x = y;
y = temp;
}void main()
{
int a = 10, b = 20;
swap(a, b);
}
執行後,並不會交換。
2、引用傳遞
void swap(int &x,int &y)
{ int temp = x;
x = y;
y = temp;
}void main()
{
int a = 10, b = 20;
swap(a, b);
printf("a=%d b=%d ", a, b);
}
執行後,發生交換。
3、指針傳遞
void swap(int *x,int *y)
{ int temp = *x; *x = *y; *y = temp;
}void main()
{
int a = 10, b = 20;
swap(&a, &b);
printf("a=%d b=%d ", a, b);
}
執行後,發生交換。
參數傳遞只有上面三種,但是如果加上數組,就會產生幾種新形式。
首先,明確數組型變數名本身只是該數組所佔存儲空間的首地址:
int a[3] = { 1, 2, 3 }; int *p = a; //等價於下行 //int *p = &a[0];
printf("%d", *p);
典型的數組做參數。
void fun(char s[]){ for (int i = 0; s[i] != '