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
3 answers

How can I copy from a c string into an array and then limit the amount returned while also adding a null terminator onto the end?

I'm trying to implement a function that copies characters from the c string source and then stores it into the array destination. I know this is strcpy however I am not allowed to call in any functions and am not allowed to use any local variables.…
ajcheng12
  • 11
  • 5
0
votes
1 answer

malloc causes

I am trying to read a file line by line, and get each line as a char * to a dynamic string, the code I am using used to work and without changing it (or noticing it), it has ceased to work, accsesing the reed information results in an error. Here is…
Yoav Raman
  • 21
  • 6
0
votes
0 answers

strcpy replace string variable after use local label in c

I have bug or error with strcpy. Everytime I goback to local label it changes "namalengkap" variables even though strcpy wasn't use for that variables. The code and output will give more explanation: int main() { FILE *fp; char…
LearnerC
  • 51
  • 6
0
votes
1 answer

How to copy from const char* variable to another const char* variable in C?

I have a string constant that is assigned to a variable, like this: const char* a = "Programming"; And I want to copy it to another variable of const char*, something like: const char* b; Here, I have tried memcpy() and strcpy(); it's not working,…
atla praveen
  • 49
  • 1
  • 4
0
votes
3 answers

Problem with while loop when I read words from a file in C

I am trying to create a program which : finds the word with largest length from a file creates a dynamically allocated 2d array stores the words from the file to array https://gist.github.com/up1047388/363854cbe703a6f297ebb644c50d307f (the whole…
0
votes
1 answer

Problem with load() function (CS50 Problem Set 5: Speller)

I'm working on CS50's Problem Set 5 (Speller) and I don't understand where I am going wrong with the load() function. I am getting errors in lines 36 and 79 when I try to compile. But I don't understand how to fix it. I am new to CS so I am sorry if…
hitsujee
  • 3
  • 1
0
votes
0 answers

Stack-Based Overflow Error(strcpy and strncpy)

I have the below code, where the compiler is complaining: int len = strlen(prgpath); char* ptr = strrchr(prgpath, '/'); char prgname[64]; memset(prgname, 0, sizeof(prgname)); if (ptr==NULL) strcpy(prgname, prgpath); else …
Santosh Sahu
  • 2,134
  • 6
  • 27
  • 51
0
votes
0 answers

How do I read a single letter from a file when the letter has a space on either side? (C)

I'm currently working on an assignment in my Intro to C class. The assignment has us using fgets to read a single line from a file, then strcpy and strtok to take individual elements from that line and store them into different arrays. The file is a…
0
votes
0 answers

strcpy mistakenly appended a struct member to another struct member within the fscanf function in a while loop

This is the code snippet from my C program struct Person { int id; char address[150]; char citi_id[11]; int phone; int type; } user, upd; int main() { ... ...; ... ...; printf("Enter id: "); scanf("%d",…
J_dash
  • 1
  • 2
0
votes
4 answers

passing string to a pointer in c

I am fairly new in C. I want to assign string in a function to a pointer but I have no idea why it is not working? This is the initial code: #include #include #include #include #include #include…
BOB
  • 1
  • 3
0
votes
1 answer

Reading string value appears as NULL

This program when it receives the input 1, it receives info from the user and if the input 2 is given it shows what it has stored so far. The issue is that when you select 2 after having inserted the required info, the name that was provided appears…
user17850871
  • 79
  • 1
  • 6
0
votes
0 answers

c: realloc(): invalid pointer

my question is simple, the code shows the problem: this works char *str1; str1 = (char *) malloc(3); strcpy(str1, "hi"); printf("%s\n", str1); str1 = realloc(str1, 5); strcpy(str1, "hi ho"); printf("%s\n", str1); this not,…
Jonas Frey
  • 65
  • 6
0
votes
2 answers

Convert String into Array of Strings in C

I'm trying to divide a string of alphabetically sorted words char *str = "a/apple/arm/basket/bread/car/camp/element/..." into an array of strings alphabetically like so: arr[0] = "a/apple/arm" arr[1] = "basket/bread" arr[2] = "car/camp" arr[3] =…
0
votes
2 answers

Using strcpy on a 2D char array allocated with malloc

I have a problem, and I cannot figure out the solution for it. I have to programm some code to a µC, but I am not familiar with it. I have to create an analysis and show the results of it on the screen of the machine. The analysis is allready done…
0
votes
2 answers

strcpy() corrupts both strings if first argument is a char pointer

Define the following variables: char *name1 = "Allan"; char name2[] = "Marco"; printf("%s %s\n", name1, name2); // Allan Marco Then the following code works fine: strcpy(name2, name1); printf("%s %s\n", name1, name2); // Allan Allan But reversing…