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
1
vote
2 answers

Unable to understand reason for segmenation fault while accessing a char string

segmentation fault after running the following code: int main(){ const char *string = "This is not reverse"; char *reverse; char temp; int length, i = 0; length = strlen(string); strcpy(reverse, string); while(i <…
codey modey
  • 983
  • 2
  • 10
  • 23
1
vote
2 answers

explanation of what my code is doing (C)

char *extractSubstring(char *str) { char temp[256]; char *subString; // the "result" printf("%s\n", str); //prints #include "hello.txt" strcpy(temp, str); //copies string before tokenizing subString = strtok(str,"\""); //…
ShadyBears
  • 3,955
  • 13
  • 44
  • 66
1
vote
1 answer

Looping and strcpy

#include #include using namespace std; int main() { int n; cout << "Enter n: "; cin >> n; cout << "Enter " << n << "names"; for(int i=0; i0"); return 0; } This is…
Supremo
  • 29
  • 3
1
vote
3 answers

Why exactly malloc is used?

I have been trying to understand what is malloc() and why is it used. I understand that malloc is for dynamic allocation of memory, it is needed if you don't know how much memory you wan't to create. I have been doing some hands-on on it. The…
Ravindra
  • 59
  • 1
  • 10
1
vote
3 answers

strcpy doesn't work on the same size arrays

When I try to assign value of one string to other using strcpy runtime error occurs. Below the code: int main (int argc, char **argv) { char str[5]; char str2[5];//if set size of str2 equal to 6, no error occurs str[0] = 'a'; str[1] =…
Nurlan
  • 2,860
  • 19
  • 47
  • 64
1
vote
2 answers

Searching through a linked list in C

I have been working on a function searchn() which takes in a linked list list and a string lname. It compiles perfectly fine, but I get a segmentation fault (core dumped) when I try running the function. I ran it through Valgrind and it tells me…
el HO
  • 57
  • 1
  • 3
  • 9
1
vote
0 answers

Return to function, difference between strcpy() and memcpy()

The paxtest program includes some interesting tests, among many others it apparently tests if strcpy and memcpy can overwrite a return pointer on the stack: (from rettofunc1.c) void doit( void ) { char buf[4]; if (strlen((const char…
csstudent2233
  • 659
  • 10
  • 17
1
vote
2 answers

Copy a char to a struct pointer char

I have 2 structs and a variable type Book Book book_struct[100]; typedef struct Book{ int id; char title[256]; char summary[2048]; int numberOfAuthors; Author * authors; }; typedef struct Author{ char firstName[56]; char…
stergosz
  • 5,754
  • 13
  • 62
  • 133
1
vote
4 answers

Strcpy: behaving more like 'strcut'

char a[3], b[3]; strcpy(a,"abc"); printf("a1 = %s\n", a); strcpy(b,a); printf("a2 = %s\n", a); printf("b = %s\n", b); From how I understand strcpy to work the output would be: a1 = abc a2 = abc b = abc Instead I obtain a1 = abc a2 = b =…
ANG-MO
  • 81
  • 1
  • 2
  • 5
1
vote
4 answers

C strtok and strcpy

I have a text file, similar to the following: Name1: ID1 Name2: ID2 Name3: ID3 I am trying to parse it to get Name1 Name2 Name3 stored in a variable. I wrote the following function: /* * filename Name of file to read * result The…
Maxim Neaga
  • 921
  • 3
  • 17
  • 29
1
vote
1 answer

function crashes when reading in a csv file

I have coded a function to read in a csv file but half way through the parsing the program crashes giving me errors in strcat.The errors are at the third field which is phone.I can't spot the error I have made in this read function.Anyone have an…
1
vote
1 answer

C Compiler Pointless Errors?

I have the following code written in C: n. struct UDSData { char *name; char *address; }; n. char UDS1[16] = "fill up sixteen", UDS2[16] = "fill up sixteen"; n. while (something) { ... 108. char…
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
1
vote
2 answers

strcpy () not working properly

I am currently writing a program where i was stuck in a different kind of behaviour of strcpy() function . Here is short demo of it... I went through the following question : Strip first and last character from C string //This program checks whether…
Subbu
  • 2,063
  • 4
  • 29
  • 42
1
vote
3 answers

Using strcpy function in C, without knowing the length

Im trying to write a simple function that get name and return it after adding an extension to it. for example, if i have the char pointer to "abcd" the function should return "abcd.as" I tried to write this function that get char pointer and return…
Yuval
  • 1,721
  • 4
  • 16
  • 15
1
vote
2 answers

Comparing 2 Strings, one in a struct other not C programming

I have this database and I Need to check whether a Product Name is already in the database otherwise I ask the user to input another one. The problem is this: I'm trying to compare a string (the Product Name) found inside the struct with the string…
DodoSombrero
  • 767
  • 3
  • 15
  • 29