Thread: correct usage of strcpy_s

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    10

    correct usage of strcpy_s

    Greetings,

    Code:
    #include "stdafx.h"#include "codificador.h"
    
    
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    
    int generaTransformacion(char * original, char * codigo) {
    
    
        int originalSize;
    
    
        if (original != NULL) {
            /* We obtain dinamically the size of the char */
            originalSize = strlen(original);                            
            /* Given that C does only pass by reference an array, we make a copy of it */
            char *originalDuplicado = (char *)malloc(originalSize * sizeof(char)); 
            strcpy_s(originalDuplicado, original);
    
    
            /* By this point, I should be able to check whether the string was copied sucessfully  */
            return strcmp(originalDuplicado, original);
        }
    }
    In this code, I've got an error in the next line
    Code:
    strcpy_s(originalDuplicado, original);
    in which the compiler, VS C++ 2017, complains about a non-existent overload instance method that matches the arguments (char *, char *)

    I know the compiler is complaining of not giving in the argument valid pointers of chars but as far as I know, I'm giving exactly those:

    Code:
    int generaTransformacion(char * original, char * codigo) {
    Code:
    char *originalDuplicado = (char *)malloc(originalSize * sizeof(char));
    I'm confused with the fact that both lines of code does not represent at least to the function strcpy_s valid char pointers.

    Any help or hint would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    You need to look up the functions you use. strcpy_s takes three parameters, not two. The second is the size of the destination buffer so that it won't be overflowed. That's the whole point of strcpy_s over strcpy.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    1. Name your files as .c, not .cpp. This is so you use a C compiler to compile your C code.
    2. You need +1 character in your malloc.
    3. You don't call free()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2018
    Posts
    10
    Thank you for your answer, you were indeed right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 10-08-2012, 06:48 AM
  2. Correct usage of malloc and free for 2D array
    By disruptivetech in forum C Programming
    Replies: 1
    Last Post: 10-20-2008, 05:20 AM
  3. Correct usage of malloc()
    By cboard_member in forum C++ Programming
    Replies: 9
    Last Post: 07-24-2005, 06:28 AM
  4. Simple Question on Correct Usage of memset
    By deadpoet in forum C++ Programming
    Replies: 2
    Last Post: 03-16-2004, 08:58 AM
  5. Simple Calculator: Not Positive About Correct Usage of C++
    By Code-Disciple in forum C++ Programming
    Replies: 6
    Last Post: 03-20-2003, 03:17 PM

Tags for this Thread