第三節(jié)(函數(shù)指針、內(nèi)存分配、字符串)

知識點

#include <iostream>

int add(int a,int b){

    return a+b;
}


int main(int argc, const char * argv[]) {
    int a=5;
    int b=3;
    int (*p)(int a,int b);
    p=add;
    int result=p(a, b);

    printf("%d \n",result);
    
    return 0;
    
}

輸出結(jié)果:

8

另一種方式

int add1(int *a,int *b)
{
    return (*a)+(*b);
}


int main(int argc, const char * argv[]) {
    int a=5;
    int b=3;
    
    int (*pp)(void *a,void *b);
    
    pp=(int(*)(void *,void*))add1;//這里要進(jìn)行強轉(zhuǎn)
    
    int result1=pp(&a,&b);
    
    printf("%d\n",result1);
    
    
    return 0;
    
}

輸出結(jié)果:

8

另一種情況

char add2(char *a,int *b)
{
    return (*a)+(*b);
}

int main(int argc, const char * argv[]) {
    char a='a';
    int b=1;
    
    int (*ppp)(void *a,void *b);
    
    ppp=(int(*)(void *,void*))add2;
    
    char result2=ppp(&a,&b);
    
    printf("%c\n",result2);
    
    
    return 0;
    
}

輸出結(jié)果:

b

說明int (*ppp)(void *a,void *b);這種定義指針的方法是比較靈活的。

注意

int *p(int a,int b);//這個p是指向一個返回值是一個int *的函數(shù)的指針

內(nèi)存

(程序區(qū)、靜態(tài)存儲區(qū)、動態(tài)存儲區(qū))

靜態(tài)存儲區(qū)

存儲的是全局變量和靜態(tài)變量,這些變量的空間在程序編譯的時候就已經(jīng)分配好了。

動態(tài)存儲區(qū)

堆區(qū):程序動態(tài)分配
棧區(qū):編譯器自動分配,由編譯器自動生成和釋放

程序的局部變量存在于(棧)中,全局變量存在于(靜態(tài)區(qū) )中,動態(tài)申請數(shù)據(jù)存在于( 堆)中。

關(guān)于static和const
const作用: “只讀(readonly)”
static:靜態(tài)變量
關(guān)于static的描述引用自這兒鏈接


1.作用于變量:

   用static聲明局部變量-------局部變量指在代碼塊{}內(nèi)部定義的變量,只在代碼塊內(nèi)部有效(作用域),其缺省的存儲方式是自動變量或說是動態(tài)存儲的,即指令執(zhí)行到變量定義處時才給變量分配存儲單元,跳出代碼塊時釋放內(nèi)存單元(生命期)。用static聲明局部變量時,則改變變量的存儲方式(生命期),使變量成為靜態(tài)的局部變量,即編譯時就為變量分配內(nèi)存,直到程序退出才釋放存儲單元。這樣,使得該局部變量有記憶功能,可以記憶上次的數(shù)據(jù),不過由于仍是局部變量,因而只能在代碼塊內(nèi)部使用(作用域不變)。

   用static聲明外部變量-------外部變量指在所有代碼塊{}之外定義的變量,它缺省為靜態(tài)變量,編譯時分配內(nèi)存,程序結(jié)束時釋放內(nèi)存單元。同時其作用域很廣,整個文件都有效甚至別的文件也能引用它。為了限制某些外部變量的作用域,使其只在本文件中有效,而不能被其他文件引用,可以用static關(guān)鍵字對其作出聲明。

  總結(jié):用static聲明局部變量,使其變?yōu)殪o態(tài)存儲方式(靜態(tài)數(shù)據(jù)區(qū)),作用域不變;用static聲明外部變量,其本身就是靜態(tài)變量,這只會改變其連接方式,使其只在本文件內(nèi)部有效,而其他文件不可連接或引用該變量。

2.作用于函數(shù):

  使用static用于函數(shù)定義時,對函數(shù)的連接方式產(chǎn)生影響,使得函數(shù)只在本文件內(nèi)部有效,對其他文件是不可見的。這樣的函數(shù)又叫作靜態(tài)函數(shù)。使用靜態(tài)函數(shù)的好處是,不用擔(dān)心與其他文件的同名函數(shù)產(chǎn)生干擾,另外也是對函數(shù)本身的一種保護(hù)機制。

  如果想要其他文件可以引用本地函數(shù),則要在函數(shù)定義時使用關(guān)鍵字extern,表示該函數(shù)是外部函數(shù),可供其他文件調(diào)用。另外在要引用別的文件中定義的外部函數(shù)的文件中,使用extern聲明要用的外部函數(shù)即可。


malloc函數(shù)

1、malloc函數(shù)的聲明:

void    *malloc(size_t __size) __result_use_check;

申請一塊大小為__size的連續(xù)的內(nèi)存空間,如果申請不成功會返回NULL;
2、calloc函數(shù)的生命:

void    *calloc(size_t __count, size_t __size) __result_use_check;

申請__count塊大小為__size的連續(xù)的內(nèi)存空間,塊與塊之間可以不用連續(xù),但是塊內(nèi)必須是連續(xù)的,如果申請不成功,則返回NULL;
動態(tài)申請的內(nèi)存使用完畢后必須進(jìn)行釋放,而且只能釋放一次:

    int *p= (int *)malloc(2*sizeof(int));
    free(p);
    p=NULL;//釋放內(nèi)存之后最好進(jìn)行置空操作

3.realloc(不常用)

void    *realloc(void *__ptr, size_t __size) __result_use_check;

字符串

    //字符串
    char c[]={'c','h','i','n','a','\0'};
    printf("%s \n",c);

從c指向的地址開始輸出,直到遇到'\0'結(jié)束。
輸出結(jié)果:

china

另一種定義方式:

    //字符串
    char c[]="china";
    printf("%s \n",c);

注意事項

    char p[10]={'c','h','i','n','a'};
    printf("%s \n",p);
    p[0]='s';
    printf("%s \n",p);

輸出結(jié)果:

china 
shina 
    char p1[]={'c','h','i','n','a','\0'};
    printf("%s \n",p1);
    
    p1[0]='s';
    printf("%s \n",p1);

輸出結(jié)果:

china 
shina 
    char *p2="china";
    printf("%s \n",p2);
    p2[0]='s';//然而這種情況卻是錯誤的,不能完成賦值操作,因為“china”存在于常量區(qū)
    printf("%s \n",p2);

第三種情況與前兩種有本質(zhì)區(qū)別,前兩種都是分配一塊區(qū)域,這塊區(qū)域的內(nèi)容是china,地址是p(或者p1),第三種情況是在常量區(qū)中有一塊區(qū)域,這塊區(qū)域的內(nèi)容是china,然后讓p2指向了這塊區(qū)域。

所以說我們可以把china從常量區(qū)拷貝一份再做修改

    char *ppp=(char *)malloc(10*sizeof(char));
    strcpy(ppp, "china");
    printf("%s\n",ppp);
    ppp[0]='s';
    printf("%s\n",ppp);

輸出結(jié)果:

china
shina

關(guān)于c語言中的一些字符串處理函數(shù)

函數(shù)名: stpcpy 
功  能: 拷貝一個字符串到另一個 
用  法: char *stpcpy(char *destin, char *source); 
程序例: 
#include <stdio.h> 
#include <string.h> 
int main(void) 
{ 
   char string[10]; 
   char *str1 = "abcdefghi"; 
   stpcpy(string, str1); 
   printf("%sn", string); 
   return 0; 
} 
  
  
  
函數(shù)名: strcat 
功  能: 字符串拼接函數(shù) 
用  法: char *strcat(char *destin, char *source); 
程序例: 
#include <string.h> 
#include <stdio.h> 
int main(void) 
{ 
   char destination[25]; 
   char *blank = " ", *c = "C++", *Borland = "Borland"; 
   strcpy(destination, Borland); 
   strcat(destination, blank); 
   strcat(destination, c); 
   printf("%sn", destination); 
   return 0; 
} 
  
  
  
函數(shù)名: strchr 
功  能: 在一個串中查找給定字符的第一個匹配之處 
用  法: char *strchr(char *str, char c); 
程序例: 
#include <string.h> 
#include <stdio.h> 
int main(void) 
 { 
    char string[15]; 
    char *ptr, c = 'r'; 
    strcpy(string, "This is a string"); 
    ptr = strchr(string, c); 
    if (ptr) 
       printf("The character %c is at position: %dn", c, ptr-string); 
    else 
       printf("The character was not foundn"); 
    return 0; 
 } 
  
  
  
函數(shù)名: strcmp 
功  能: 串比較 
用  法: int strcmp(char *str1, char *str2); 
看Asic碼,str1>str2,返回值 > 0;兩串相等,返回0
程序例: 
#include <string.h> 
#include <stdio.h> 
int main(void) 
 { 
    char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc"; 
    int ptr; 
    ptr = strcmp(buf2, buf1); 
    if (ptr > 0) 
       printf("buffer 2 is greater than buffer 1n"); 
    else 
       printf("buffer 2 is less than buffer 1n"); 
    ptr = strcmp(buf2, buf3); 
    if (ptr > 0) 
       printf("buffer 2 is greater than buffer 3n"); 
    else 
       printf("buffer 2 is less than buffer 3n"); 
    return 0; 
 } 
  
  
  
函數(shù)名: strncmpi 
功  能: 將一個串中的一部分與另一個串比較, 不管大小寫 
用  法: int strncmpi(char *str1, char *str2, unsigned maxlen); 
程序例: 
#include <string.h> 
#include <stdio.h> 
int main(void) 
{ 
   char *buf1 = "BBB", *buf2 = "bbb"; 
   int ptr; 
   ptr = strcmpi(buf2, buf1); 
   if (ptr > 0) 
      printf("buffer 2 is greater than buffer 1n"); 
   if (ptr < 0) 
      printf("buffer 2 is less than buffer 1n"); 
   if (ptr == 0) 
      printf("buffer 2 equals buffer 1n"); 
   return 0; 
} 
  
  
  
函數(shù)名: strcpy 
功  能: 串拷貝 
用  法: char *strcpy(char *str1, char *str2); 
程序例: 
#include <stdio.h> 
#include <string.h> 
int main(void) 
 { 
    char string[10]; 
    char *str1 = "abcdefghi"; 
    strcpy(string, str1); 
    printf("%sn", string); 
    return 0; 
 } 
  
  
  
函數(shù)名: strcspn 
功  能: 在串中查找第一個給定字符集內(nèi)容的段 
用  法: int strcspn(char *str1, char *str2); 
程序例: 
#include <stdio.h> 
#include <string.h> 
#include <alloc.h> 
int main(void) 
 { 
    char *string1 = "1234567890"; 
    char *string2 = "747DC8"; 
    int length; 
    length = strcspn(string1, string2); 
    printf("Character where strings intersect is at position %dn", length); 
    return 0; 
 } 
  
  
  
函數(shù)名: strdup 
功  能: 將串拷貝到新建的位置處 
用  法: char *strdup(char *str); 
程序例: 
#include <stdio.h> 
#include <string.h> 
#include <alloc.h> 
int main(void) 
 { 
    char *dup_str, *string = "abcde"; 
    dup_str = strdup(string); 
    printf("%sn", dup_str); 
    free(dup_str); 
    return 0; 
 } 
  
  
  
函數(shù)名: stricmp 
功  能: 以大小寫不敏感方式比較兩個串 
用  法: int stricmp(char *str1, char *str2); 
程序例: 
#include <string.h> 
#include <stdio.h> 
int main(void) 
{ 
   char *buf1 = "BBB", *buf2 = "bbb"; 
   int ptr; 
   ptr = stricmp(buf2, buf1); 
   if (ptr > 0) 
      printf("buffer 2 is greater than buffer 1n"); 
   if (ptr < 0) 
      printf("buffer 2 is less than buffer 1n"); 
   if (ptr == 0) 
      printf("buffer 2 equals buffer 1n"); 
   return 0; 
} 
  
  
函數(shù)名: strerror 
功  能: 返回指向錯誤信息字符串的指針 
用  法: char *strerror(int errnum); 
程序例: 
#include <stdio.h> 
#include <errno.h> 
int main(void) 
{ 
   char *buffer; 
   buffer = strerror(errno); 
   printf("Error: %sn", buffer); 
   return 0; 
} 
  
  
  
函數(shù)名: strcmpi 
功  能: 將一個串與另一個比較, 不管大小寫 
用  法: int strcmpi(char *str1, char *str2); 
程序例: 
#include <string.h> 
#include <stdio.h> 
int main(void) 
{ 
   char *buf1 = "BBB", *buf2 = "bbb"; 
   int ptr; 
   ptr = strcmpi(buf2, buf1); 
   if (ptr > 0) 
      printf("buffer 2 is greater than buffer 1n"); 
   if (ptr < 0) 
      printf("buffer 2 is less than buffer 1n"); 
   if (ptr == 0) 
      printf("buffer 2 equals buffer 1n"); 
   return 0; 
} 
  
  
  
函數(shù)名: strncmp 
功  能: 串比較 
用  法: int strncmp(char *str1, char *str2, int maxlen); 
程序例: 
#include <string.h> 
#include <stdio.h> 
int  main(void) 
{ 
   char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc"; 
   int ptr; 
   ptr = strncmp(buf2,buf1,3); 
   if (ptr > 0) 
      printf("buffer 2 is greater than buffer 1n"); 
   else 
      printf("buffer 2 is less than buffer 1n"); 
   ptr = strncmp(buf2,buf3,3); 
   if (ptr > 0) 
      printf("buffer 2 is greater than buffer 3n"); 
   else 
      printf("buffer 2 is less than buffer 3n"); 
   return(0); 
} 
  
  
函數(shù)名: strncmpi 
功  能: 把串中的一部分與另一串中的一部分比較, 不管大小寫 
用  法: int strncmpi(char *str1, char *str2); 
程序例: 
#include <string.h> 
#include <stdio.h> 
int main(void) 
{ 
   char *buf1 = "BBBccc", *buf2 = "bbbccc"; 
   int ptr; 
   ptr = strncmpi(buf2,buf1,3); 
   if (ptr > 0) 
      printf("buffer 2 is greater than buffer 1n"); 
   if (ptr < 0) 
      printf("buffer 2 is less than buffer 1n"); 
   if (ptr == 0) 
      printf("buffer 2 equals buffer 1n"); 
   return 0; 
} 
  
  
函數(shù)名: strncpy 
功  能: 串拷貝 
用  法: char *strncpy(char *destin, char *source, int maxlen); 
程序例: 
#include <stdio.h> 
#include <string.h> 
int main(void) 
{ 
   char string[10]; 
   char *str1 = "abcdefghi"; 
   strncpy(string, str1, 3); 
   string[3] = ''; 
   printf("%sn", string); 
   return 0; 
} 
  
  
函數(shù)名: strnicmp 
功  能: 不注重大小寫地比較兩個串 
用  法: int strnicmp(char *str1, char *str2, unsigned maxlen); 
程序例: 
#include <string.h> 
#include <stdio.h> 
int main(void) 
{ 
   char *buf1 = "BBBccc", *buf2 = "bbbccc"; 
   int ptr; 
   ptr = strnicmp(buf2, buf1, 3); 
   if (ptr > 0) 
      printf("buffer 2 is greater than buffer 1n"); 
   if (ptr < 0) 
      printf("buffer 2 is less than buffer 1n"); 
   if (ptr == 0) 
      printf("buffer 2 equals buffer 1n"); 
   return 0; 
} 
  
  
  
函數(shù)名: strnset 
功  能: 將一個串中的所有字符都設(shè)為指定字符 
用  法: char *strnset(char *str, char ch, unsigned n); 
程序例: 
#include <stdio.h> 
#include <string.h> 
int main(void) 
{ 
   char *string = "abcdefghijklmnopqrstuvwxyz"; 
   char letter = 'x'; 
   printf("string before strnset: %sn", string); 
   strnset(string, letter, 13); 
   printf("string after  strnset: %sn", string); 
   return 0; 
} 
  
  
函數(shù)名: strpbrk 
功  能: 在串中查找給定字符集中的字符 
用  法: char *strpbrk(char *str1, char *str2); 
程序例: 
#include <stdio.h> 
#include <string.h> 
int main(void) 
{ 
   char *string1 = "abcdefghijklmnopqrstuvwxyz"; 
   char *string2 = "onm"; 
   char *ptr; 
   ptr = strpbrk(string1, string2); 
   if (ptr) 
      printf("strpbrk found first character: %cn", *ptr); 
   else 
      printf("strpbrk didn't find character in setn"); 
   return 0; 
} 
  
  
  
函數(shù)名: strrchr 
功  能: 在串中查找指定字符的最后一個出現(xiàn) 
用  法: char *strrchr(char *str, char c); 
程序例: 
#include <string.h> 
#include <stdio.h> 
int main(void) 
{ 
   char string[15]; 
   char *ptr, c = 'r'; 
   strcpy(string, "This is a string"); 
   ptr = strrchr(string, c); 
   if (ptr) 
      printf("The character %c is at position: %dn", c, ptr-string); 
   else 
      printf("The character was not foundn"); 
   return 0; 
} 
  
  
  
函數(shù)名: strrev 
功  能: 串倒轉(zhuǎn) 
用  法: char *strrev(char *str); 
程序例: 
#include <string.h> 
#include <stdio.h> 
int main(void) 
{ 
   char *forward = "string"; 
   printf("Before strrev(): %sn", forward); 
   strrev(forward); 
   printf("After strrev():  %sn", forward); 
   return 0; 
} 
  
函數(shù)名: strset 
功  能: 將一個串中的所有字符都設(shè)為指定字符 
用  法: char *strset(char *str, char c); 
程序例: 
#include <stdio.h> 
#include <string.h> 
int main(void) 
{ 
   char string[10] = "123456789"; 
   char symbol = 'c'; 
   printf("Before strset(): %sn", string); 
   strset(string, symbol); 
   printf("After strset():  %sn", string); 
   return 0; 
} 
  
  
  
函數(shù)名: strspn 
功  能: 在串中查找指定字符集的子集的第一次出現(xiàn) 
用  法: int strspn(char *str1, char *str2); 
程序例: 
#include <stdio.h> 
#include <string.h> 
#include <alloc.h> 
int main(void) 
{ 
   char *string1 = "1234567890"; 
   char *string2 = "123DC8"; 
   int length; 
   length = strspn(string1, string2); 
   printf("Character where strings differ is at position %dn", length); 
   return 0; 
} 
  
  
函數(shù)名: strstr 
功  能: 在串中查找指定字符串的第一次出現(xiàn) 
用  法: char *strstr(char *str1, char *str2); 
程序例: 
#include <stdio.h> 
#include <string.h> 
int main(void) 
{ 
   char *str1 = "Borland International", *str2 = "nation", *ptr; 
   ptr = strstr(str1, str2); 
   printf("The substring is: %sn", ptr); 
   return 0; 
} 
  
  
函數(shù)名: strtod 
功  能: 將字符串轉(zhuǎn)換為double型值 
用  法: double strtod(char *str, char **endptr); 
程序例: 
#include <stdio.h> 
#include <stdlib.h> 
int main(void) 
{ 
   char input[80], *endptr; 
   double value; 
   printf("Enter a floating point number:"); 
   gets(input); 
   value = strtod(input, &endptr); 
   printf("The string is %s the number is %lfn", input, value); 
   return 0; 
} 
  
  
  
函數(shù)名: strtok 
功  能: 查找由在第二個串中指定的分界符分隔開的單詞 
用  法: char *strtok(char *str1, char *str2); 
程序例: 
#include <string.h> 
#include <stdio.h> 
int main(void) 
{ 
   char input[16] = "abc,d"; 
   char *p; 
   /* strtok places a NULL terminator 
   in front of the token, if found */ 
   p = strtok(input, ","); 
   if (p)   printf("%sn", p); 
   /* A second call to strtok using a NULL 
   as the first parameter returns a pointer 
   to the character following the token  */ 
   p = strtok(NULL, ","); 
   if (p)   printf("%sn", p); 
   return 0; 
} 
  
  
  
函數(shù)名: strtol 
功  能: 將串轉(zhuǎn)換為長整數(shù) 
用  法: long strtol(char *str, char **endptr, int base); 
程序例: 
#include <stdlib.h> 
#include <stdio.h> 
int main(void) 
{ 
   char *string = "87654321", *endptr; 
   long lnumber; 
   /* strtol converts string to long integer  */ 
   lnumber = strtol(string, &endptr, 10); 
   printf("string = %s  long = %ldn", string, lnumber); 
   return 0; 
} 
  
函數(shù)名: strupr 
功  能: 將串中的小寫字母轉(zhuǎn)換為大寫字母 
用  法: char *strupr(char *str); 
程序例: 
#include <stdio.h> 
#include <string.h> 
int main(void) 
{ 
   char *string = "abcdefghijklmnopqrstuvwxyz", *ptr; 
   /* converts string to upper case characters */ 
   ptr = strupr(string); 
   printf("%sn", ptr); 
   return 0; 
} 
  
  
  
函數(shù)名: swab 
功  能: 交換字節(jié) 
用  法: void swab (char *from, char *to, int nbytes); 
程序例: 
#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
char source[15] = "rFna koBlrna d"; 
char target[15]; 
int main(void) 
{ 
   swab(source, target, strlen(source)); 
   printf("This is target: %sn", target); 
   return 0; 
}



PS:isalpha()是字符函數(shù),不是字符串函數(shù),

isalpha 
  
 

  原型:extern int isalpha(int c);
  
  用法:#include <ctype.h>
  
  功能:判斷字符c是否為英文字母
  
  說明:當(dāng)c為英文字母a-z或A-Z時,返回非零值,否則返回零。
  
  舉例:

      // isalpha.c
      
      #include <syslib.h>
      #include <ctype.h>
      #include <stdio.h>

      main()
      {
        int c;
        
        clrscr();        // clear screen
        printf("Press a key");
        for(;;)
        {
          c=getchar();
          clrscr();
          printf("%c: %s letter",c,isalpha(c)?"is":"not");
        }
        return 0; // just to avoid warnings by compiler
      }


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 幾種語言的特性 匯編程序:將匯編語言源程序翻譯成目標(biāo)程序編譯程序:將高級語言源程序翻譯成目標(biāo)程序解釋程序:將高級語...
    囊螢映雪的螢閱讀 3,066評論 1 5
  • 家里的活全落在母親一人的肩上,除了燒飯、做菜、洗碗、洗衣!喂豬、養(yǎng)鵝一一切飼料。夏播,秋收還要到隊里曬谷,自家曬稻...
    清風(fēng)流水_5e84閱讀 403評論 4 13
  • hi 大家好我是暴躁老哥 今天給大家分享干貨,如何拍攝帶貨視頻,把貨賣出去呢? 要實現(xiàn)拍視頻帶貨,直播帶貨,人氣是...
    錦鯉_bdd4閱讀 1,388評論 0 4
  • 天光云影, 寂寞春河冷。 疑是蘆叢情淚耿, 不忍醉中喚醒。 春深一葦爭青, 群芳鶴立亭亭。 但恨無人悅賞, 奈何說...
    青魚吹浪閱讀 513評論 0 6
  • 現(xiàn)在,今天晚上,他不是以一個看了幾本哲學(xué)書后頗有想法的年輕人的身份 來寫作此篇,今晚他更像是一個小男孩,需要情感上...
    sai118cool閱讀 148評論 0 0

友情鏈接更多精彩內(nèi)容