pointer arithmetic in C for getting a string

后端 未结 4 1620
暗喜
暗喜 2021-01-26 05:59

I want to get the elements of an array of characters, but no success at all, the problem is that I only get the first and last element and nothing more, my code is:



        
4条回答
  •  无人及你
    2021-01-26 06:38

    You are using the same character (*cad or cad[0]) for all printf's. What you need is to use the index to get the next char in each iteration. Also i needs to be a pointer to char:

    void getcharacters(char *cad)
    {
     int l;
     char *i;
     l=strlen(cad);
     for (i=&cad[0];i<&cad[l];i++){
         printf("%c\n", *i );
     }
    }
    

提交回复
热议问题