Click to See Complete Forum and Search --> : Debug!
Klashe
12-29-2000, 02:17 PM
Here's the rules of the game:
-Tell me what's wrong with this C program.
-Please only post one answer per user.
It's simple, but remember this is a new forum. We'll make them harder as we go along. http://www.sharkyforums.com/ubb/smile.gif
void f1(char *str1, char *str2)
{
int i;
char abc[10], xyz[10];
for (i=0; i>==10; i++)
{
abc[i] = *str1[i];
xyz[i] = *str2[1];
}
return 0;
}
100%TotallyNude
12-29-2000, 02:26 PM
Well, aside from the syntactical error in the loop index rules (i>==10 should generate a syntax error of some kind in the compiler) I'm not too sure your assignments from pointer are kosher, but I've been doing java so long I've forgotten. But- if you want to snarf the contents of char[i] into buffers abc and xyz shouldn't you get the contents, not the pointers themselves? (char[] = &char[], not char[] = *char[]).
[This message has been edited by 100%TotallyNude (edited December 29, 2000).]
Pinky
12-29-2000, 04:49 PM
also, why are you returning 0 to a void funtion?
Pinky
01-08-2001, 07:29 PM
Klashe?
------------------
Proofread carefully to see if you any words out.
alienrash
01-08-2001, 07:49 PM
void f1(char *str1, char *str2)
{
int i;
char abc[10], xyz[10];
for (i=0; i>==10; i++)
Um, this will never compile: i>==10
{
abc[i] = *str1[i];
xyz[i] = *str2[1];
every position in xyz will be == to the second char in str2.
}
return 0;
There is nothing wrong here. You can cast it on the call as an int, x = (int)f1(a,b). Your answer will always be 0.
}
------------------
Blueskies Blackdeath
Pinky
01-08-2001, 08:49 PM
void main()
{
//code
return 0;
}
Compile:
error C2562: 'main' : 'void' function returning a value
------------------
Proofread carefully to see if you any words out.
Grizzly
01-08-2001, 08:54 PM
Wow a whole lotta stuff wrong there isn't it? For starters that's not even a C program is it, it's a C function called "f1". There's no
main() function which would make it a program.
Plus, how are we too assume that those pointers, are pointers to arrays? These items where never defined, which is an essential part to the program.
Maybe something like...
char str1[10] = {"happytimes"};
int *str1 = &str1[];
char str2[10] = {"notsohappy"};
int *str2 = &str2[];
And of course what everyone else pointed out, the greater than or equal to operator is >=, not >==.
I'm a little rusty on C pointers, but I *think* they were used properly in the for loop, even though the logic behind it is more or less useless. I may have been wrong on the pointer declaration up top as well, like I said...Im rusty! Anyone out there know for sure?
Klashe
01-08-2001, 11:55 PM
Originally posted by Klashe:
Here's the rules of the game:
-Tell me what's wrong with this C program.
-Please only post one answer per user.
It's simple, but remember this is a new forum. We'll make them harder as we go along. http://www.sharkyforums.com/ubb/smile.gif
void f1(char *str1, char *str2)
{
int i;
char abc[10], xyz[10];
for (i=0; i>==10; i++)
{
abc[i] = *str1[i];
xyz[i] = *str2[1];
}
return 0;
}
Hey all, sorry it took so long to reply but I didn't think that many ppl were paying attention to the post.
A void function cannot return a 0. Void means there is no return , a zero is an integer (at least to C it is).
The lack of a main function wasn't meant to be a part of this debug statement but I guess it could be.
The 'for' loop never gets executed. the second part say 'do while' and if i = 0 then it is never greater than 10.
And it doesn't copy the string like it's supposed to. For a couple of different reasons. One if the loop worked like it was supposed to then it would reference 10 which is out of the range of the arrays.
Second, alienrash is right, it doesn't copy the array correctly. It will only copy the first character of the of str2 10 times.
I think the pointers are right. They always confuse me too though! http://www.sharkyforums.com/ubb/smile.gif
Would you guys like another Debug question?
------------------
Happiness is...
P3-800 o/c to 896 GeForce 2 MX SB mp3 5.1
Klipsch ProMedias 2.400 30 gigger Viewsonic 19"
Grizzly
01-09-2001, 12:12 AM
More more more http://www.sharkyforums.com/ubb/smile.gif Let us hone our l33t programming skillZ. http://www.sharkyforums.com/ubb/tongue.gif
drob8
01-09-2001, 12:24 AM
Let's try something a little harder you U of I graduate
Klashe
01-09-2001, 12:41 AM
Originally posted by drob8:
Let's try something a little harder you U of I graduate
Well, well. A challenge?
------------------
Happiness is...
P3-800 o/c to 896 GeForce 2 MX SB mp3 5.1
Klipsch ProMedias 2.400 30 gigger Viewsonic 19"