electic102 Posted July 30, 2004 Share Posted July 30, 2004 what is wrong with this: void func1{ char Data[1]; Data[0] = 1; Data[1] = 2; func2(Data); } func2(char *input) { printf("%s\n", Data); } Problem is that input in func2 comes wiht a "" string instead of having the data? Any ideas? Link to comment Share on other sites More sharing options...
0 Glowstick Posted July 30, 2004 Share Posted July 30, 2004 What you posted there wouldn't even compile because of func2, how about feeding input as parameter to printf. Additionally you're writing two bytes in an one byte char array, can you say buffer overrun? Third of all, you didn't terminate the char array with 0, so if the memory where it's allocated isn't nulled, it'll dump as much as possible until it hits a 0. Link to comment Share on other sites More sharing options...
0 electic102 Posted July 30, 2004 Author Share Posted July 30, 2004 I dont care about the data area. It is not a buffer overrun. The data fills fine in func's data structure. The problem is in passing. In any case, the code above is not meant to compile but show a concept. Link to comment Share on other sites More sharing options...
0 Glowstick Posted July 30, 2004 Share Posted July 30, 2004 Well, badly written concept. So I am going to assume you're passing the stuff as in your "example". Try: func2(&Data); Take notice of the ampersand. Since your func2 wants a pointer. Link to comment Share on other sites More sharing options...
0 Andareed Posted July 30, 2004 Share Posted July 30, 2004 Why don't you fix the obvious problem(s) pointed out by Tom and then see what happens? No one is going to help you debug code if you don't attempt to fix the bugs. Link to comment Share on other sites More sharing options...
0 alpha_omega Posted July 30, 2004 Share Posted July 30, 2004 what is wrong with this:void func1{ char Data[1]; Data[0] = 1; Data[1] = 2; func2(Data); } func2(char *input) { printf("%s\n", Data); } Problem is that input in func2 comes wiht a "" string instead of having the data? Any ideas? how can you assign 2 values to an array which has only enough storage for one element? When declaring an array, you enter the max. size n in brackets. But the "indexing" starts at 0. So your interval is 0 to n-1. Link to comment Share on other sites More sharing options...
0 GatorV Posted July 31, 2004 Share Posted July 31, 2004 Also I think you need to allocate space for the null character '\0' so you can print your data Link to comment Share on other sites More sharing options...
0 bithub Posted August 2, 2004 Share Posted August 2, 2004 Here is a more correct version of your code: // Declare this function so func1 can see it void func2(char *input); void func1(){ char Data[3]; // Declare space for 3 characters Data[0] = '1'; Data[1] = '2'; Data[2] = 0; // Null terminate the string // Note: // The above lines could be done as // char Data[] = "12"; func2(Data); } void func2(char *input) { printf("%s\n", input); } Well, badly written concept. So I am going to assume you're passing the stuff as in your "example".Try: func2(&Data); Take notice of the ampersand. Since your func2 wants a pointer. That is incorrect. func2(&Data) would be passing a pointer to a pointer. The way he had it was fine. I dont care about the data area. It is not a buffer overrun. The data fills fine in func's data structure. The problem is in passing. Actually what you have IS a buffer over-run. As alpha_omega pointed out, you only allocated enough space for 1 character, but you tried to put in 2 characters. Link to comment Share on other sites More sharing options...
Question
electic102
what is wrong with this:
void func1{
char Data[1];
Data[0] = 1;
Data[1] = 2;
func2(Data);
}
func2(char *input) {
printf("%s\n", Data);
}
Problem is that input in func2 comes wiht a "" string instead of having the data? Any ideas?
Link to comment
Share on other sites
7 answers to this question
Recommended Posts