-1

This is my code

char function(char *dst)
{
    int i;
    char *arr;

    i = 0;
    while(dst[i] != '\0')
    {
        arr[i] = dst[i];
        i++;
    }
    dst[i] != '\0'
    return(arr);
}

int main(void)
{
    char a[] ="asdf"
    printf("%s", function(a);
}

I want to copy *dst to empty *arr but my code didn't work. I can't understand. How can I copy array without inner function in C(ex_strcpy, memspy....) Thank you

asio_guy
  • 3,667
  • 2
  • 19
  • 35
JB P
  • 86
  • 1
  • 10
  • I don't understand what you think "inner function" means, nor why you want to avoid them. Anyway, no function that you use for the copying can help you here, because the problem is that you do not properly understand what's happening in the underlying memory. You can't just arbitrarily pick somewhere to copy the string *to*. You have to *allocate* memory. – Karl Knechtel Apr 05 '21 at 03:22
  • Please read through http://c-faq.com/malloc/index.html and also http://c-faq.com/aryptr/index.html . – Karl Knechtel Apr 05 '21 at 03:23
  • @JB P Hint: When `arr[i]` is first called, what is the value of pointer `arr`? – chux - Reinstate Monica Apr 05 '21 at 03:36
  • your code won't compile, unless you fix this `char a[] ="asdf"` – asio_guy Apr 05 '21 at 04:04
  • `char *arr;` not allocated `arr[i] = dst[i]` will go kaboom – asio_guy Apr 05 '21 at 04:05
  • @u__: You have spent time edit his code, why still pointed out the missing `;`? – NeoZoom.lua Apr 05 '21 at 04:51
  • You can use `memcpy()` like in https://stackoverflow.com/questions/5952512/memcpy-string-and-terminator – ralf htp Apr 05 '21 at 06:19

5 Answers5

1

Apart from missing ; and making sure that the string being passed to the function is always a '\0' terminated one ( else the program will run into side effects strcpy causes ). and returning char* instead of char, you missed allocating memory for arr

// return char * instead of char
char* function(char *dst)
{
    // Note - sizeof(dst) wont work
    // Neither does sizeof(dst)/sizeof(char)
    // allocate one extra for '\0'
    size_t size_to_alloc = (strlen(dst) + 1) * (sizeof *arr);
    char *arr = malloc( size_to_alloc  );
    char *p = arr;
    for ( ; *dst ; p++, dst++)
         *p = *dst;
    *p = '\0';
    return(arr);
} 
asio_guy
  • 3,667
  • 2
  • 19
  • 35
0

If you want to dynamically copy an array, you'll need to allocate memory for the char array using malloc or other equivalent. Make sure you free the memory once you're done with it. I would suggest reading some posts on malloc and allocating memory in c.

This is probably a good place to start.

https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/

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

char* function(char *dst, size_t length) {
    int i;
    // Allocating the memory needed for the char array.
    char *arr = (char*) malloc (sizeof(char) * length);

    i = 0;
    while(dst[i] != '\0') {
        arr[i] = dst[i];
        i++;
    }
    arr[length - 1] = '\0';
    return(arr);
}

int main(void) {
    char a[] ="asdf";
    // Getting length of the array
    size_t length = sizeof(a) / sizeof(a[0]);
    char* val = function(a, length);
    printf("%s", val);
    free(val);
}

chengcsy
  • 11
  • 1
  • 3
0

If there is a possibility of copying strings without using c functions, perhaps it can be done by doing what c functions do.

it may be interesting to see what strcpy does: https://code.woboq.org/userspace/glibc/string/strcpy.c.html

char *
STRCPY (char *dest, const char *src)
{
  return memcpy (dest, src, strlen (src) + 1);
}

infact it uses memcpy: https://code.woboq.org/gcc/libgcc/memcpy.c.html

and here the magic...

void *
memcpy (void *dest, const void *src, size_t len)
{
  char *d = dest;
  const char *s = src;
  while (len--)
    *d++ = *s++;
  return dest;
}

and strlen: https://code.woboq.org/userspace/glibc/string/strlen.c.html

Mario Abbruscato
  • 819
  • 1
  • 7
  • 9
0

You are missing the memory allocation and basically attempting to recode strdup. See below:

char    *ft_strdup(const char *src)
{
    char    *dst;
    int     len;

    len = 0;
    while (src[len]) // no inner function
        ++len;
    if (!(dst = malloc(sizeof(char) * (len + 1)))) // need 1 extra char to NULL terminate.
        return NULL;
    dst[len] = '\0';
    while (--len > -1)
        dst[len] = src[len];
    return dst;
}

Note that it makes sense to code your own version of strdup and include it in your program library as this function is not part of the C Standard.

Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81
0

You can use memcpy() to copy memory directly, like in Memcpy, string and terminator and https://www.gnu.org/software/libc/manual/html_node/Copying-Strings-and-Arrays.html In C any string has to be terminated by \0 (sentinel value)

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

int main()
{
  char source[] = "World";   
  char destination[] = "Hello ";   
  /* Printing destination string before memcpy */
  printf("Original String: %s\n", destination);
  /* Copies contents of source to destination */
  memcpy (destination, source, sizeof(source)); 
  /* Printing destination string after memcpy */
  printf("Modified String: %s\n", destination);

  return 0;
}

source : https://www.educative.io/edpresso/c-copying-data-using-the-memcpy-function-in-c

ralf htp
  • 9,149
  • 4
  • 22
  • 34