for example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char substr[10][20];
int main() {
substr[0] = "abc";
printf("%d", substr[0]);
}
of course above is wrong? how to do it? thanks
You can't assign strings like that in C. Instead, use strcpy(substr[0], "abc"). Also, use %s not %d in your printf
I hate giving 'full' answers to homework questions, but the only way to answer your question is to show you the answer to your problem, because it is so basic:
#include <stdio.h>
#include <string.h>
#define MAXLEN 20
char substr[10][MAXLEN];
int main(void) {
strncpy(substr[0], "abc", MAXLEN);
puts(substr[0]);
return 0;
}
Your code (as is) has several faults:
You are treating substr[0] as a string literal. You can't do that.
You were using printf formatters incorrectly. %s is for strings
You don't need to bother printf() to print a string
You should (in real code) watch out for buffer overflows, hence strncpy()
If main() doesn't want / need argc and argv, its arguments should be void
main() should return a value, as its return type is int
You aren't using anything out of <stdlib.h>, why include it?
I suggest researching string literals, the functions available in <string.h> as well as format specifiers.
Also note, I am not checking the return of strncpy(), which is something you should be doing. That is left as an exercise for the reader.
Hope this helps:
void main(void)
{
char* string[10];
string[0] = "Hello";
}
Otherwise I think ya need to copy it by hand or use strcpy or the like to move it from one block of memory to another.
Related
So I have this vulnerable piece of code in which I'm trying to exploit to print out Come on in.. Below is the code mentioned.
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int user_age;
void secretClub(){
if(user_age<18){
printf("Come back when your old enough!!");
}
else{
printf("Come on in.");
}
}
int main(){
char name[30];
user_age = 17;
gets(name);
printf("Hello there ");
printf(name);
}
Now, I'm aware that since the function secretClub is never called, I would need to overflow the buffer name in so that the return address points at the address for secretClub. I'm also aware that there exists a format string vulnerability in printf in main that could be exploited to change the variable user_age.
I'm struggling to see how you would piece this all together. Is it possible that the use of a format string attack is actually redundant here and you could just redirect with a buffer overflow so that it prints out "Come on in."?
I'm new to this and still learning, I'd really appreciate any form of guidance. Thank you!
How can I make the "while" function stop when the i position is a blank space?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
int i,t;
char array[20];
t=strlen(array);
for (i=0;i<=t;i++)
{
if(isspace(expressio[i])){
while (expressio[i+1]!=isspace(expressio[i+1]))
i=i+1;
if (isspace(expressio[i+1]))
{ code follows here...}
Can I do it like this?
I am programming in C.
No, you can't do this.
while (expressio[i+1]!=isspace(expressio[i+1]))
makes no sense. expressio[i+1] is a character, while isspace(expressio[i+1]) is either 1 or 0. You compare apples to oranges.
Besides,
char array[20];
t=strlen(array);
makes no sense, either. If you write a string to the array, then strlen would return the length of that string. Calling strlen before initializing array is useless (and it may return just anything).
I am working on a school assignment and I need a little bit of help.
My question is, how can I compose characters that are read in from a file into a single string of memory. Here is what i have so far.
My Code:
#include <stdio.h>
#include <stdlib.h>
char myString;
int main(int argc, char *argv[]){
FILE* fin;
char ch;
fin=fopen(argv[1],"r");
while((ch=fgetc(fin))!=EOF){
printf("%c\n", ch);
}
fclose(fin);
return 0;
}
My teacher said the last part of main is to be:
putchar(ā\nā);
printf( myString );
return 0;
}
But I'm not sure how to put that within my code. Thank you ahead of time, Im also not looking to be just given the answer if you could help me work it out that would be great thank you again.
Updated code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
FILE* fin;
int i;
char myString[3];
fin=fopen(argv[1],"r");
while(fgets(myString,sizeof(myString), fin)){
putchar('\n');
printf("%c\n", myString[i]);
}
fclose(fin);
return 0;
}
Im unsure if this code is exactly correct. It prints out the items within the file and puts a space between them. and there is an array being used for the string.
Im also not looking to be just given the answer
Fine.
Define a char array (mystring) large enough to hold your string
Define a counter to keep track of the position in the array
At each step store ch into the array
Remember to 0-terminate it (store 0 as the last element).
Things to note:
You will need to learn about realloc and grow the storage as you go if your program is to read arbitrarily long input. Better leave that for later
It's generally unsafe to printf(dynamicstr). What if it contains a "%d" ? It's better to printf("%s", dynamicstr).
You get to learn about memcpy and malloc. They are your friends.
--Jason
You need to read the applicable part of your textbook that explains what a "string" is in C. Without you understanding that, there's almost no way to answer your question without simply doing it for you.
A C "string" is a contiguous chunk of memory containing chars and is NULL terminated. You'll need to allocate that then place the characters into it as you read them from the file. Note that you have to make sure you don't go beyond the memory you've allocated while doing so, this is called a "buffer overflow".
#include <stdio.h>
#include <stdlib.h>
char* myString;
int main(int argc, char *argv[]){
FILE* fin;
fpos_t fsize = 0;
char ch;
char *pch;
fin=fopen(argv[1],"r");
fseek(fin,0,SEEK_END);
fgetpos(fin, &fsize);
rewind(fin);
pch = myString = (char*)malloc((fsize+1)*sizeof(char));
while((ch=fgetc(fin))!=EOF){
*pch++ = (char)ch;
}
*pch = '\0';
fclose(fin);
printf("%s", myString);//printf(myString);// need escape %
free(myString);
return 0;
}
So I have the following test code:
#include <string.h>
#include <stdio.h>
int main(int argc, char* argv[]){
int retVal = strcmp("", "");
printf("%d\n", retVal);
return 0;
}
And for me, it always seems print out 0, i.e. "" and "" are always equal one another. But I'm curious. Is this something guaranteed by strcmp, or does it have the potential to vary from implementation to implementation? Maybe I'm just being paranoid, but I've worked on enough strange systems to know the perils of differing implementations.
UPDATE:
I've decided to clarify to justify my paranoia. What I'm really doing in my program is more akin to this:
#include <string.h>
#include <stdio.h>
int doOperation(const char* toCompare){
//do stuff in here
int compResult = strcmp(toCompare, "");
//do more stuff depending on compResult
}
int main(int argc, char* argv[]){
const char* myString = "";
doOperation(myString);
return 0;
}
I want to make sure things in doOperation will proceed correctly. Note that this is just an example. In my doOperation function I'm not going to actually know that the value of toCompare.
A string is equal to another string if all the characters before the NULL terminator of both strings are exactly the same. Since "" has no characters, it fits that definition when compared with "".
No it won't vary with different implementations as C code is compiled to machine specific code and strcmp() will do the same in all platforms. You will get the same result everywhere. I also agree with Seth Carnegie's answer.
āāI have a file copy program that takes from one file and pastes in another file pointer. But, instead of getting targetname from user input i'd like to just add a '1' at the end of the input filename and save. So, I tried something like this...
.... header & inits ....
fp=fopen(argv[1],"r");
fq=fopen(argv[1].'1',"w");
.... file copy code ....
Yeah it seems stupid but I'm a beginner and need some help, do respond soon. Thanks :D
P.S. Want it in pure C. I believe the dot operator can work in C++.. or atleast i think.. hmm
One more thing, i'm already aware of strcat function.. If i use it, then i'll have to define the size in the array... hmm. is there no way to do it like fopen(argv[1]+"extra","w")
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* stradd(const char* a, const char* b){
size_t len = strlen(a) + strlen(b);
char *ret = (char*)malloc(len * sizeof(char) + 1);
*ret = '\0';
return strcat(strcat(ret, a) ,b);
}
int main(int argc, char *argv[]){
char *str = stradd(argv[1], "extra");
printf("%s\n", str);
free(str);
return 0;
}
Have a look at strcat:
An example:
#include <string.h>
char alpha[14] = "something";
strcat(alpha, " bla"); // "something bla"
printf("%s\n", alpha);
Using the dot won't work.
The function you are looking for is called strcat.
Unfortunately . would not work in c++.
A somewhat inelegant but effective method might be to do the following.
int tempLen=strlen(argv[1])+2;
char* newName=(char*)malloc(tempLen*sizeof(char));
strcpy(newName,argv[1]);
strcat(newName,"1");
In C to concatenate a string use strcat(str2, str1)
strcat(argv[1],"1") will concatenate the strings. Also, single quotes generate literal characters while double quotes generate literal strings. The difference is the null terminator.