Questions tagged [strcpy]

The C standard library function, "strcpy()," is used to copy non-overlapping, null-terminated strings. It is also defined as "std::strcpy()" in the C++ standard library.

Used to copy null-terminated, non-overlapping strings, it is defined in the <string.h> standard header. It is also defined in the C++ standard library in the <cstring> header.

Documentation for C strcpy.

Documentation for C++ std::strcpy.

947 questions
0
votes
2 answers

Why doesn't while (*s++=*t++); work for me?

I came across this function on a blog and I found it really cool. I understand how it works conceptually since C++ was my first language. However, when I tried actually writing it out in a program of mine, it doesn't seem to work. I've googled all…
Prashanth Chandra
  • 2,672
  • 3
  • 24
  • 42
0
votes
5 answers

why the function strcat not working?

I have some code in C, I want to connect the ssid with the string "option" in the for loop void ApListCallback(ScanResult *pApList) { int i; printf("Find %d APs: \r\n", pApList->ApNum); for (i=0;iApNum;i++){ …
chanjianyi
  • 607
  • 4
  • 15
  • 35
0
votes
4 answers

Why calling strcpy() leads to changes of my source string?

main() { char a[]="abss"; char c[]=""; strcpy(c,a); printf("%s",a); } Why does the source string a change on using strcpy() it is checked only when string c is greater than or equal to string a??
0
votes
4 answers

Overflow not detected when writing nul character in middle of string?

Say I have the code: char* word = malloc (sizeof(char) * 6); strcpy(word, "hello\0extra"); puts(word); free(word); This compiles just find and Valgrind has no issue, but is there actually a problem? It seems like I am writing into memory that I…
Foundry
  • 33
  • 7
0
votes
1 answer

Copying command line strings to an array, valgrind errors

I am expecting names from the command line and I need to hold them in an array. The multiple ways I've tried aren't working (I'm using strcpy) and I'm getting the following valgrind errors: Use of uninitialised value of size 8 ==23027== at…
Cody P
  • 3
  • 2
0
votes
2 answers

How do I return a character array from a function? Then use the returned char in another function?

char * read_command() { char command[25]; char *input = malloc(sizeof(char) * 25); printf("myRolodex Command: "); scanf("%c", &command); strcpy(input, command); return input; } void…
Stephanie
  • 13
  • 1
  • 3
0
votes
2 answers

Making strcpy function with linked list in c

I was making my own strcpy function using linked list but couldn't get how to do. Without using linked list it could be like this char* cp2014strcpy(char * dest_ptr, const char * src_ptr) { char* strresult = dest_ptr; if((NULL != dest_ptr)…
0
votes
1 answer

C - strcpy appears to be concatenating successive values stored in a struct

It's been a while since I have used C and am finding some issues in my code. I have a struct as such: struct packet { char seq[4]; char type[4]; char src[10]; char dst[10]; char payload[MAX_BUF_LEN]; //predefined buffer length constant } Then when…
CChiste
  • 131
  • 1
  • 2
  • 13
0
votes
2 answers

Replacing part of string with another string causes segfault

I want to do something simple but I've been banging my head on this for too long. I have a string that will always end with a specific "token". In the case below "++". I want to replace the ending token with a shorter token, let's say "#". strstr…
7 Reeds
  • 2,419
  • 3
  • 32
  • 64
0
votes
2 answers

split string in C and take each field separately

I want to split a string i have "msg 10 2" into different strings & ints. so instead of having msg 10 2 I can take each as a seperate parameter can print: msg 10 2 I use the variable to define a message: char msg[30] = "msg 10…
user3035890
0
votes
2 answers

Concatenating a char in a recursive function in C

Even though I have more experience with higher level languages, I am having a lot of troubles understanding how memory allocation and how strings really work in C. I am trying to implement a very simple base converter that works recursively. The…
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69
0
votes
1 answer

allocate extra memory for a char but not for an int in an already allocated struct?

I got this error message: passing argument 1 of x makes integer from pointer without a cast. I have done exaclty the same way when passing an int and that worked perfectly. But when I did it with an char I got this error. Code: struct x { char…
0
votes
2 answers

Assertion Failed error while trying to create a substring

I'm trying to write my own function in order to substring a given pointer to chars array, by a given length and start index. When I execute the code, i'm getting an error "Debug Assertion Failed". The program failes in "tcscpy_s.inl" (Expression:…
Aviv
  • 456
  • 1
  • 7
  • 16
0
votes
2 answers

Why can't I dynamically allocate memory of this string of a struct?

Let's say for example, I have a struct: typedef struct person { int id; char *name; } Person; Why can't I do the following: void function(const char *new_name) { Person *human; human->name = malloc(strlen(new_name) + 1); }
assaf
  • 69
  • 2
  • 10
0
votes
2 answers

Having trouble adding an int to a string, tried using sprintf but I'm having trouble

I am trying to read a file and print all of the words that are in the file, ignoring all other spaces and symbols. I have it working with strcpy but it's giving me an error and I'm trying to use sprintf but I don't really understand how that…
Kot
  • 11
  • 4