It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
In this code I tried to write a function which returns 0 value if the two strings don't match, and a length of matching characters if i can find a substring in str that wholey resambles patt.
#include....
int check(char *str, char *patt, int *b)
{
if (*str == *patt && *patt != 0)
return *b * (1 + check(str+1,patt+1,&b));
else if (*patt == 0)
return 0;
else{
*b = 0;
return 0;
}
}
main()
{
char s1[SIZE] = "mama";
char s2[SIZE] = "mama";
int b = 1;
printf("%d\n",check(s1,s2,&b));
b = 1;
system ("pause");
return;
}
Here I should get the output 4, but I get -77779463.
Thanks for help!
PS I used recursion and set the b parameter as changable.
b is already a pointer to an int, so you want to make the recursive call with b and not &b.
Any decent compiler, with warnings enabled, would have alerted you to that mistake!
return *b * (1 + check(str+1,patt+1,&b));
^ dont pass address.
Pass b to it instead.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to Write a function in C language whose output with respect to input should like this:
The above table is just for an example. The input is not limited to 25, and also the number of inputs in a particular range is X instead of 5. I cannot figure out how to do this?
Right now I don't have enough time write a better question ;). Please edit it if you found any mistake.
int f(int x, int X){
return (x + (X-1))/X;
}
int func(int x)
{
if(x%5 == 0)
return x/5;
else
return x/5 + 1;
}
What about an array of structs along
struct range {
int lo, hi, result;
}
Ask the user for X, then allocate an array with X instances of this struct,
#include <stdlib.h>
struct range *array = malloc (X * sizeof *array);
Now loop over X table rows asking for the lo, hi and result. The rest is left as an exercise...
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Can anyone help me convert the C code below to MIPS code? I'm supposed to use recursion and get 21 - 2 as the final answer. Thanks!
/*
x is a pointer to a linked list node and is not null.
Return the min value stored in the linked list.
Assume each node is stored in memory as an int (value) followed by a pointer to the next node (next), each a word wide.
You must write it with recursion. */
int findMin(node *x) {
if(x->next == NULL)
return x->value;
else {
int min = findMin(x->next);
if(min < x->value)
return min;
else
return x->value;
}
}
Here you are:
mips-linux-gnu-gcc -S -o foo.asm foo.c
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Original page: http://acm.whu.edu.cn/learn/problem/detail?problem_id=1036
This should be a simple Dynamic Programming problem. I figured out the solution to be the following:
int main(void)
{
double d[501];
int i;
d[0] = d[1] = 1.;
d[2] = 2.;
for(i = 3; i<=500; i++)
d[i] = d[i-1] + d[i-2] + d[i-3];
int n;
while(scanf("%d", &n) == 1) {
if(n == 0) return 0;
printf("%.0lf\n", d[n]);
}
return 0;
}
But Wrong Answer reported after submission. I really don't know why.
double is not enough for the precision
you should use high-precision to solve it
Decimal point maybe. printf("%.0lf\n", 1.0); will print 1.0 but system may wait for 1.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
My order is defined as 'A < a < B < b ...< Z < z'.
I have to find if a given string is in range or not.
Ex. If my range is AaA - BaB, AA or AaaB is inthe range, but not CbAA.
I am looking for any pointers, ideas, suggestions to help me start. I will implement this in C.
So all you need to implement is a single function that compares two strings according to your rules. It is kind of modified lexicograogical sorting:
int compare_letters(char x, char y) {
char lx = tolower(x);
char ly = tolower(y);
if (lx != ly) {
return lx < ly;
} else {
return x < y;
}
}
int smaller(const char* a, const char* b) {
.. use the above function ...
}
Now make use of the above function and to check if a given string x is in the range (a,b), check if smaller(a, x) and smaller(x, b). That's it.
Some tips on the function smaller - compare the strings char by char and if the two chars differ, return their compare_letter. If one of the strings runs out of letters, consider it smaller.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have this piece of code that I need to modify to demonstrate integer overflow vulnerability. I have never done it before and need a head start.
#include <stdio.h>
int myprintf(char* argv){
printf("%s\n", argv);
return 0;
}
int myprintf2(char* argv){
printf("hello world\n");
return 0;
}
int main(int argc, char** argv){
struct foodata{
int (*fptr)(char*);
int buf[4];
} foo;
foo.buf[0] = 0xdeadbeef;
foo.fptr = myprintf;
foo.buf[0xffffffff] = myprintf2;
foo.fptr(argv[1]);
return 0;
}
Ok. So your code, at least on some 32-bit platforms, does print hello world\n and not the argument from the user. We have changed the function pointer by manipulating other array. Now we want to use it for malicious purpose.
First thing - we replace myprintf2 with something dangerous, when called without some checking, like:
void set_authorized(void) {
authorized = 1;
}
Now, we need to have to get some input from user. For example we will read four numbers and use their sum as index to buf, when the input seems valid.
int a, b, c, d;
do {
scanf("%d%d%d%d", &a, &b, &c, &d);
} while (a < 0 || b < 0 || c < 0 || a + b + c > 4);
buf[a+b+c] = d;
Looks like we could never write outside the array, right?
No, the attacker can use data
a = INT_MAX;
b = 1;
c = INT_MAX;
d = (int)set_authorized
INT_MAX + 1 + INT_MAX = INT_MIN + INT_MAX = -1 (0xffffffff). So we basically have behavior like in your example, but this time it grants unprivileged user some rights.
Note:
To have this example working on 64-bit platform replace int buf[4]; with long buf[4] and 0xffffffff with 0xffffffffffffffff.