RicardasSim

str pointer arg

Apr 6th, 2019
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void testas(char *a, char *b)
  6. {
  7.    unsigned int i = 0;
  8.  
  9.    while(b[i])
  10.    {
  11.       a[i]=b[i];
  12.       i++;
  13.    }
  14. }
  15.  
  16. void testas2(char *a, char *b)
  17. {
  18.    memcpy(a, b, strlen(b));
  19. }
  20.  
  21. void testas3(char *a, char *b)
  22. {
  23.    unsigned int  ln = strlen(b);
  24.  
  25.    if(ln < 4) return;
  26.  
  27.    *a=*b;
  28.    *(a+1)=*(b+1);
  29.    a[2]=b[2];
  30.    memcpy(a+3, b+3, ln-3);
  31.  
  32. }
  33.  
  34. int main (int argc, char **argv){
  35.  
  36.    char tstStr[]="Testas";
  37.  
  38.    printf("%s\n",tstStr);
  39.  
  40. {
  41.    char* str;
  42.    
  43.    str = malloc(strlen(tstStr)+1);
  44.  
  45.    if(!str)
  46.    {
  47.       printf("error (1) !\n");
  48.       return 1;
  49.    }
  50.  
  51.    memset(str,0,strlen(tstStr)+1);
  52.  
  53.    testas(str,tstStr);
  54.  
  55.    printf("%s\n",str);
  56.  
  57.    free(str);
  58. }
  59.  
  60. {
  61.    char* str;
  62.    
  63.    str = malloc(strlen(tstStr)+1);
  64.  
  65.    if(!str)
  66.    {
  67.       printf("error (2) !\n");
  68.       return 1;
  69.    }
  70.  
  71.    memset(str,0,strlen(tstStr)+1);
  72.  
  73.    testas2(str,tstStr);
  74.  
  75.    printf("%s\n",str);
  76.  
  77.    free(str);
  78. }
  79.  
  80. {
  81.    char* str;
  82.    
  83.    str = malloc(strlen(tstStr)+1);
  84.  
  85.    if(!str)
  86.    {
  87.       printf("error (3) !\n");
  88.       return 1;
  89.    }
  90.  
  91.    memset(str,0,strlen(tstStr)+1);
  92.  
  93.    testas3(str,tstStr);
  94.  
  95.    printf("%s\n",str);
  96.  
  97.    free(str);
  98. }
  99.  
  100. return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment