Home > Software design >  Add element by element in array pointer from buffer pointer in for loop
Add element by element in array pointer from buffer pointer in for loop

Time:06-24

How to add element from string character array to another character array by pointers in c? below I've given code, please correct it and suggest anything because I didn't got a satisfied answer, I just want to same string in 'copy' array from 'buffer' array by only using there pointers?

    char buffer[5] = "stop";    // Buffer character array
    char copy[5];               // Copy character empty array

    // Pointers
    char *buffer_ptr, *copy_ptr;

    buffer_ptr = buffer;
    copy_ptr = copy;

    int i;
    for ( i = 0; i < 4; i  )
    {
        strncpy(copy_ptr, buffer_ptr, 1);   // Here I want to copy string from buffer_pointer to copy_ptr
        buffer_ptr = buffer_ptr   1;        // Here buffer_pointer pointer address is up by 1    
        copy_ptr = copy_ptr   1;            // Here copy_pointer pointer address is up by 1

    }    
    printf("%s\n", copy);
    return 0; 
}

CodePudding user response:

It almost looks like you are trying to invent a string manipulation method that already exists in standard code. You actually are utilizing the "strcpy" function in your code. Following is a simplified version of what I believe you are attempting to do.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char buffer[5] = "stop";    // Buffer character array
    char copy[5];               // Copy character empty array
    
    strcpy(copy, buffer);
    
    printf("%s\n", copy);
    
    return 0;
}

If, for some reason, you need to manipulate the data from your "buffer" string before the characters are placed into the "copy" string (e.g. make upper or lower case), you could utilize a method as follows.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char buffer[5] = "stop";    // Buffer character array
    char copy[5];               // Copy character empty array

    for (int i = 0; i < strlen(buffer); i  )
    {
        /* If needed, some processing could be done to the buffer character */
        copy[i] = buffer[i];
    }

    printf("%s\n", copy);
    
    return 0;
}
    

I hope that gives you some food for thought.

Regards.

CodePudding user response:

For starters the array buffer contains 5 characters including the terminating zero character '\0' of the string literal "stop"

char buffer[5] = "stop";    // Buffer character array

So if you are going to copy character by character then the condition of the for loop should be written like

for ( i = 0; i < 5; i  )
             ^^^^^

Also if you are going to use pointers to copy a string in an array then the variable i is redundant.

Calling the function strncpy to copy only one character looks unnatural.

You could just write the for loop for example the following way

char buffer[5] = "stop";    // Buffer character array
char copy[5];               // Copy character empty array

for ( char *buffer_ptr = buffer, *copy_ptr = copy; 
      ( *copy_ptr   = *buffer_ptr   ) != '\0'; );

printf("%s\n", copy);
  • Related