2

I am trying to modify a string in C language

    char signal_cat[8]; 

        if (k == 1) {
            strcpy_s(signal_cat, "HPHA",6);             //why cant I change char array (string) values???
        }
        else if (k == 2) {
            strcpy_s(signal_cat, "Normal",6);
        }

        printf("Original signal category: %s \n", signal_cat);

When I run this it shows an exception "Unhandled exception at 0x7BEBF71D (ucrtbased.dll) in Lab3Parti.exe: 0xC0000005: Access violation reading location 0x00000006"

I have tried

signal_cat = "HPHA";

too, but an error shows "expression must be a modifiable lvalue"

Does anyone know how I can go about doing this?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
WXYZ
  • 53
  • 2
  • 9
  • 8
    You got those parameters the wrong around. It's the destination, then the size of the destination, and then the source. – Blaze Feb 18 '20 at 12:59
  • Didn't you get compiler warnings? If so, consider them as errors. – Jabberwocky Feb 18 '20 at 13:02
  • 1
    Note that `strcpy_s()` is a *de facto* non-portable, Microsoft-only extension. `strcpy()` is **NOT** "deprecated" in any manner by anyone other than Microsoft, and `strcpy_s()` being "safer" is arguable at best. – Andrew Henle Feb 18 '20 at 13:24
  • Yet another case where `strcpy` was simple and correct whereas adding in "safer" version with extra arguments causes problem. – M.M Feb 18 '20 at 20:06

3 Answers3

5

Are you using Visual Studio to compile your C code?

If you are and the compiler is forcing you to use strcpy_s() instead of strcpy(), you can still use the standard library function strcpy() by defining the following macro at the top of your source file:

#define _CRT_SECURE_NO_WARNINGS

The actual problem in your code is that you are giving the arguments to strcpy_s() in the wrong order. Check the function's prototype to provide the arguments in the correct order.

machine_1
  • 4,266
  • 2
  • 21
  • 42
4

You specified an invalid order of arguments. A call of strcpy_s should look like

strcpy_s(signal_cat, sizeof( signal_cat ), "HPHA" );

Otherwise use the standard C function strcpy like

strcpy( signal_cat, "HPHA" );

provided that the array signal_cat has enough space to accommodate the string literal.

Or you can use another standard function strncpy

strncpy( signal_cat, "HPHA", sizeof( signal_cat ) );
signal_cat[sizeof( signal_cat )-1] = '\0';

As for this statement

signal_cat = "HPHA";

then arrays do not have the assignment operator. They are non-modifiable lvalues.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-2

What is strcpy_s? Do you have a prototype for it in your code (ie. have you included the correct files for declarations?) What are its arguments, and why are you using it?

You are obviously learning C; it is not in your best interest to start with some vendors proprietary (and often bizarre) extensions? Learn the language, learn its established (standard) interfaces, then, if absolutely necessary, endure some vendors twisted extensions is a good process to follow:

#include <string.h>
#include <stdio.h>
int main(void) {
  char signal_cat[8]; 
  strncpy(signal_cat, "HPHA", sizeof signal_cat);
  printf("%s\n", signal_cat);
  strncpy(signal_cat, "Normal", sizeof signal_cat);
  printf("%s\n", signal_cat);
  return 0;
}
mevets
  • 10,070
  • 1
  • 21
  • 33
  • 2
    Be aware that `strncpy` is not a somewhat safer version of `strcpy`. `strncpy` may leave you with a non NUL terminated string.... – Jabberwocky Feb 18 '20 at 13:23
  • Actually `strncpy` should __never__ be used, it's a leftover from the early days and it should never have made it into string.h in the first place. – Jabberwocky Feb 18 '20 at 13:30
  • Not Never, it should be used with caution as it performs a very precise function which is often misinterpreted. strlcpy() is the better generic, but solves a different problem and has *only* been in use for 20 or so years. – mevets Feb 18 '20 at 14:57
  • `strncpy`is basically useless. – Jabberwocky Feb 18 '20 at 15:04
  • "which is often misinterpreted" - as in this answer – M.M Feb 18 '20 at 20:07