All Questions
191 questions
1
vote
1
answer
126
views
Why does this C code not print 'a' in output?
#include<stdio.h>
#include<string.h>
void main(){
char x[9]="helloabc";
x[8]='\0';
int a=2,b=3;
strcpy(&x[2],&x[4]);
for(int i=0;i<8;i++){
printf(...
0
votes
1
answer
73
views
User input sanitization program, which takes a specific amount of arguments and passes the execution to a bash script
I am trying to write a little program, which is resistant against buffer overflow and similar vulnerabilities.
Since we cannot trust the user input, I thought it would be a good idea to concatenate ...
2
votes
3
answers
74
views
Is it possible to create a character array within a strcpy?
Is it possible to copy a string using C's strcpy without first assigning memory to a char *character_array?
Basically, is something like this possible:
strcpy(char *my_string, another_string);
where ...
0
votes
1
answer
72
views
How can I fix this error which causes my other string not to print?
I am trying to run the program below where one contains a date and the other contains the clothes in an outfit. The date structure works fine, but the one about the outfit results in the following ...
3
votes
2
answers
243
views
Why do i gett warnings by using strcpy_s and strcat_s?
I programmed an example on my Win64 PC with code:blocks using strcpy_s and strcat_s. The program works, but I get the warning messages "warning: implicit declaration of function 'strcpy_s' for ...
0
votes
1
answer
78
views
I don't understand why am I getting this output in this C program
#include <stdio.h>
#include <string.h>
int main(void) {
char s1[6] = "Hello";
char s2[3];
strcpy(s2, s1);
printf("%s\n", s2);
return 0;
}
...
-4
votes
1
answer
63
views
Strcpy thru array subscribed
In the Ritchie/Kernighan book, they show a few ways how to create a self made strcpy function, one of them is with using array subscribed instead of char pointers , but when I try this method and run ...
2
votes
3
answers
467
views
Why does strncpy() produce garbage when the dest is smaller than the src but large enough to fit the wanted substring of src?
I tried to limit the number of n bytes copied to dest (here str1) by using strncpy().
The dest is big enough for the n bytes, but the output produced garbage when dest is smaller than the source (here ...
0
votes
1
answer
447
views
malloc causes <Access violation writing location>
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 ...
-3
votes
1
answer
77
views
How do I initiate multiple strings in malloc in C
I'm trying to learn C and I have an asignment to use malloc and struct and I have it print out the queue number but the string wont print. I have attached a picture of the print, but only works when ...
0
votes
2
answers
1k
views
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/...
0
votes
0
answers
18
views
String concatenates with another after strcpy for the latter [duplicate]
As you can see in my code bellow I don't mess with e[i].politico in the for cycle where my problem occurs, but for some reason, after perfoming a strcpy onto e[i].risco of a predifined char array, e[i]...
1
vote
2
answers
700
views
Is this snprintf method a safe way to copy strings?
I need your advice. I use this way to copy strings knowing a max size to not go over but some of these strings don't end with a null terminator. It's just a snippet.
void my_strcpy(char* dest, const ...
0
votes
1
answer
415
views
Properties of strcpy()
I have a global definition as following:
#define globalstring "example1"
typedef struct
{
char key[100];
char trail[10][100];
bson_value_t value;
} ObjectInfo;
typedef ...
-2
votes
1
answer
230
views
simple strcpy from string to string crashes entire programme [duplicate]
sorry if this is a commonly asked question but I'm not sure as to why this code will not output anything:
#include <stdio.h>
#include <string.h>
int main()
{
char source[] = "hello ...
0
votes
2
answers
68
views
C -Implementing strcpy() But Segfault
I have made a strcpy() function
in C, and I am copying words from one array to other not just letters, but when I run it I am getting Segmentation fault what to do?
#include <stdio.h>
void ...
-1
votes
1
answer
685
views
Input 50 characters into a string in C
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define _CRT_SECURE_NO_WARNINGS
int main(void){
//declare 3 char arrays to hold 3 strings of 20 characters each.
char ...
-1
votes
1
answer
196
views
Split a string to an array of strings, along with a flag
I'm trying to write a code in c that return 1 if there is "&" in the string and 0 otherwise.
In addition, the char* that I receive in the function I want to put it in an array of chars ...
0
votes
1
answer
116
views
Regarding structs in C, how to copy the data from a struct to a string array
I have a program if finds servers on the network using mDNS. It is from a opensource stack.
Currently, I am in need of guidance for the following use case.
USECASE: Whenever I run the program to ...
0
votes
2
answers
523
views
EXC_BAD_INSTRUCTION using "strcpy" within a function
When I use the whole code in the main function it works perfectly but now I want to use the function for some "Strings" which I initialize in a 2D-Array.
The idea behind the function is to ...
1
vote
1
answer
688
views
Trying to use the strcpy_s in vsc?
When I try to run the code i get:
warning: implicit declaration of function 'strcpy_s' [-Wimplicit-function-declaration]
I included string.h and stdio.h.
Code:
#include <string.h>
#include <...
2
votes
3
answers
290
views
C: What size should you allocate to a string array to be passed to strcpy, to be copied into.?
If I need to copy a string, src into the array, dest, using strcpy or strncpy, should I allocate an arbitrarily large sized array (like char dest[1024] for example) or should I calculate the size of ...
1
vote
2
answers
272
views
strcpy problem with two dimension array in C
I'm trying to copy a string to an array of strings using the strcpy function but it doesn't work!
Here is the code:
char *message[10] = { "Hello!000000000000", "Good Bye!", "...
-2
votes
3
answers
102
views
String Implementation of a strcpy
I am supposed to implement a strcpy function without using the library. I have written the code for that but there seems to be an error (possibly a memory leak but I'm not sure)
But if any of you can ...
0
votes
1
answer
111
views
Failed to use strcpy() in stack
this is a code about push strings and pop strings from a stack, the string name must be fixed (not input by user). But the code is unable to run (it cannot display anything). And the point is because ...
-1
votes
1
answer
99
views
converting specific part of string to integer
*updated with how i stored the string into my struct
I have a struct as below:
struct patient {
char name[30], ID[8];
int age, phoneNo;
};
and i've written the following code:
int searchName(...
2
votes
2
answers
3k
views
What should be the right usage of strncpy_s() - secure string copy - to handle all the possible corner cases?
I got to know about the function - strncpy_s(), which is called the secure version of string copy. More secure than the functions - strcpy() and strncpy().
strncpy_s() is described more at this link -
...
-2
votes
2
answers
338
views
How to join characters?
#include <stdio.h>
#include <string.h>
int main(void) {
char rez[100] = "\0";
char t = 97;
char temp;
strcpy(temp, t);
strcat(rez, temp);
printf("%...
3
votes
6
answers
1k
views
How to properly implement strcpy in c?
According to this:
strcpy vs strdup,
strcpy could be implemented with a loop, they used this while(*ptr2++ = *ptr1++). I have tried to do similar:
#include <stdio.h>
#include <stdlib.h>
...
0
votes
3
answers
5k
views
C catching strcat buffer overflow
This subprogram takes three user inputs: a text string, a path to a file, and a 1 digit flag. It loads the file into a buffer, then appends both the flag and the file buffer, in that order, to a char ...
0
votes
0
answers
60
views
Segmentation fault caused by strcpy to an initialized variable
I am expanding the kilo
text editor to include copying-and-pasting. I am getting a segfault, which I have narrowed down to be because of a strcpy. I am using a typedef to store information about the ...
1
vote
1
answer
229
views
Need guidance with aspects of strcpy in C
This code is from strcpy in string.c
I am trying to understand a few of the features here:
char *(strcpy)(char *restrict s1, const char *restrict s2)
{
char *dst = s1;
const char *src = ...
2
votes
2
answers
2k
views
How strcpy changes the value of string
I got this homework to decide what the following code will do (on paper, without testing on computer).
char s1[]="Short Message Service", *s2, *s3;
s2=strchr(s1,'M');
s3=strchr(s2,'S');
strncpy(s1+1,...
0
votes
1
answer
494
views
C: copy string into list of strings
So I have a list of names and corresponding phone numbers, and I want the user to be able to continuously enter a new name-number pair into that list. The part of my code where I try to do that looks ...
-3
votes
3
answers
2k
views
Use strcpy() or strncpy() for array of strings?
I'm struggling to copy a string within an array at a given index to another array of strings, any suggestions? When trying to print out the value of tempVal at any given index, it doesn't return ...
0
votes
2
answers
238
views
Why does strcpy change the value of this one parameter?
Hi this code I created capitalizes lower case letters. strcpy manages to copy the value of string to stringTwo, however I was wondering why strcpy changes the value of string as well if I just used ...
-1
votes
2
answers
61
views
comparing string to words in an array
I got an assignment from my teacher to write a code that compares a given word to a bunch of words located in an array of strings.
If the word in the array is lexicography smaller than the word given, ...
1
vote
3
answers
2k
views
Why strcpy_s is safer than strcpy?
When I am trying to use the strcpy function the visual studio gives me an error
error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation,...
0
votes
2
answers
215
views
Use of strcpy() in pointers to string in C
I'm trying to use strcpy() with pointers to strings and after a successful compilation when I run it it gives error. I don't know why this is happening.
int main()
{
char *s1="abcd";
...
0
votes
1
answer
514
views
Strcpy() not copying the second character in a string
I have been running into issues with the strcpy() function in C. In this function I take a string in buffer and the string contains a something along the lines of '(213);' and I am trying to remove ...
0
votes
1
answer
673
views
Assigning value to a character array in a struct
I am trying to assign a Character array in struct using a string literal as shown below but it is not working:
s1.name[20] = "Mark";//prints some garbage string with special symbols
But when I tried ...
2
votes
1
answer
73
views
How and why exactly does char* s2 change after the last strcpy() call?
This is one of the questions that may end up on a test I'm going to have in a couple of days at school. I'm having trouble with understanding how char* s2 changes from "Message Service" to "ice" after ...
3
votes
5
answers
2k
views
Why does strcpy omit the first character in source string when using destination string of size 1?
In C the strcpy function is used to copy a source into a destination string.
But when I use a destination char array of size 1 the strcpy correctly copies the source into the destination. But it also ...
4
votes
1
answer
194
views
strcpy vs direct assignment: Overflow issue
This is a practice question from my school, it's not a homework question:
Given the following declaration, write a snippet of C code that might
lead to strlen(arr) returning no less than 8.
char ...
3
votes
5
answers
4k
views
C - memcpy with char * with length greater than source string length
I have the following code in C now
int length = 50
char *target_str = (char*) malloc(length);
char *source_str = read_string_from_somewhere() // read a string from somewhere
...
0
votes
4
answers
2k
views
Trying to copy the remainder of a string after a character
I am trying to copy the remainder of a user inputted string. It is always input in the following: airport, 00:00, 00:00. The numbers are flight times and layover times. In my code I've gotten it to ...
0
votes
0
answers
259
views
Using strcpy to copy part of a string after a certain character
so I'm having the user input a destination, flight time, and a layover time. The input will always look like "destination, 0:00, 0:00" and I'm trying to copy the input and reprint it. I have some ...
1
vote
3
answers
3k
views
Why does this implementation of 'strncpy' work?
I have to rewrite for an assignment a function that mimics the behavior of the strncpy, after many trials and error, some external help, here's the final code :
15 char *ft_strncpy(char *dest, ...
-2
votes
3
answers
1k
views
C Reason why strncpy not have null termination [closed]
I'm currently learning strings in C, and a question arose: why doesn't the C developers make strncpy null terminated automatically?
For example in the implementation of strncpy, why didn't people ...
0
votes
2
answers
213
views
C strcpy() copies string literals without segmentation fault
To my understanding string literals are stored in read-only memory and modifying it during runtime leads to a segmentation fault, but my below code compiles without a segmentation fault.
#include <...