C
Chad
Given the following code that achieves no useful purpose:
#include <string.h>
#include <stdio.h>
#include <string.h>
int manip(char *str) {
size_t len = strlen(str)-1;
if(len >= 3) {
str[0] = 'A';
str[1] = 'B';
printf("The length of the string is: %d\n", len);
}
else {
return -1;
}
}
int main(int argc, char **argv){
if(argc !=2){
fprintf(stderr,"Not enough arguements\n");
exit(1);
}
manip(argv[1]);
printf("The modified value is: %s\n",argv[1]);
argv[1] = NULL;
printf("The new modified value is: %s\n",argv[1]);
return 0;
}
I really don't know how to word this in any graceful way. Please bear
with this. How is it possible to accidently modify the string in
argv[1]? I can maybe see something like malloc() returning NULL, then
maybe like having this value be passed to manip(), but other than that,
really see this.
Thanks in advance
Chad
#include <string.h>
#include <stdio.h>
#include <string.h>
int manip(char *str) {
size_t len = strlen(str)-1;
if(len >= 3) {
str[0] = 'A';
str[1] = 'B';
printf("The length of the string is: %d\n", len);
}
else {
return -1;
}
}
int main(int argc, char **argv){
if(argc !=2){
fprintf(stderr,"Not enough arguements\n");
exit(1);
}
manip(argv[1]);
printf("The modified value is: %s\n",argv[1]);
argv[1] = NULL;
printf("The new modified value is: %s\n",argv[1]);
return 0;
}
I really don't know how to word this in any graceful way. Please bear
with this. How is it possible to accidently modify the string in
argv[1]? I can maybe see something like malloc() returning NULL, then
maybe like having this value be passed to manip(), but other than that,
really see this.
Thanks in advance
Chad