While (isspace(array)) - c

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).

Related

Calling a function doesn't execute the print statement inside that function

Why is it that my first block of code doesn't output anything while the 2nd block does print out "hi"? I suspect that the program never goes into the test() function in the first block, but I don't know why that happens and how I should fix it.
P.S. I know that my codes don't really make any sense such as that the return value of the functions should be char *. This is because I haven't completed the function and I am still at the stage of testing what I have written. I think the return value of char * shouldn't be the reason for my problem, but do let me know if it is!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* test(char **strs, int strs_sz){
printf("%s", "hi");
}
int main(void){
char *arg[] = {"XX", "YY", "ZZ"};
char *all = test(arg, 1);
printf("%s\n", all);
free(all);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* fcn(char **para){
printf("%s", "hi");
}
int main(void){
char *arg[] = {"XX", "YY", "ZZ"};
char *s = fcn(arg);
}
To sum up what was already explained in the comments and fill in some blanks:
When using standard C library functions to work with files (printf actually works with a file stdout), the data is first cached in memory, and only written to the actual file when some condition is met:
The file is closed
The file is fflushed.
The memory buffer is full
etc.
In case of stdout, this will happen when the \n character is printed or when your program exists and the file is closed.
However, in your first code snippet you try to dereference (use) a pointer all.
Since you did not write a return statement in your test function, it is impossible to predict what value will end up being stored in all.
So, your program most likely crashes unexpectedly, and thus the buffer never gets written to stdout.
You should never test incomplete functions!
At the very least, build up a skeleton code that makes the function legal, such as a dummy return statement.
Otherwise, you will encounter "undefined behavior", which is another way of saying your program will react in weird and unpredictable ways.

How would you change a global variable with a buffer overflow?

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!

C strcat - warning: passing arg 2 of `strcat' makes pointer from integer without a cast

I'm having a problem with the program below. I'm trying to scan through a string command entered by the user for certain words. My major issue right now is that when I run the following I get a warning saying that "passing arg 2 of `strcat' makes pointer from integer without a cast". My intent is to loop through the first three characters of the string "s", concatenate them onto a string "firstthree", and later check the value of the string "firstthree". Any help is appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>
/* Simple example of using gnu readline to get lines of input from a user.
Needs to be linked with -lreadline -lcurses
add_history tells the readline library to add the line to it's
internal histiry, so that using up-arrow (or ^p) will allows the user
to see/edit previous lines.
*/
int main(int argc, char **argv) {
char *s;
while (s=readline("Enter Name: ")) {
add_history(s); /* adds the line to the readline history buffer */
printf("Hello %s\n",s);/*output message to the user*/
char *firstthree;
int i;
for(i = 0; i < 3; i++){
strcat(firstthree, s[i]);
printf("Hello %s\n",firstthree);//checking to see the character added to the end of the string
}
printf("Hey %s\n",firstthree);/*prints out the first three characters*/
free(s); /* clean up! */
free(firstthree);
}
return(0);
}
Your program has a lot of problems; you never initialize firstthree, for example.
The reason you're getting the specific error you're seeing is because of this call:
strcat(firstthree, s[i]);
s is a char *, so s[i] is a char, but strcat expects both parameters to be pointers to null-terminated strings. What it seems you want is something like:
char firstthree[4] = { 0 };
for (int i = 0; i < 3; i++)
{
firstthree[i] = s[i];
printf("Hello %s\n", firstthree);
}
You can't use strcat() to do this; it requires two char* s as arguments, not a char* and a char. You could use strncat() if it is available on your platform.

Comparing "" and "" in C

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.

how to assign a value to a string array?

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.

Resources