
Structure pointers, char, and passing
Quote:
>I'm having a slight problem with some structures I'm trying to pass
>around in functions as pointers. When I run the program under gdb, and
<snip>
Quote:
>that the memory address is non-existant when trying a 'print'). I know
>I'm doing something wrong...but what?
This is my analysis ( [HS] ):
typedef struct httpd_con {
int bleh;
char bah[200];
Quote:
}; /* [HS] ';' was missing */
main () {
struct httpd_con* hc;
/* [HS] 'http_con' undefined !
'struct' add,
'httpd_con' modified to 'httpd_con'
(you should compile your code before posting
and paste it from you source editor.
These are typos ! (I hope so...) */
hc->bleh = 1;
/* [HS] Compiler complains :
"Possible use of 'hc' before definition"
hc is undefined. It must be initialised withe the address
of a valid data block. */
strcpy(hc->bah, "hi!");
/* [HS] Compiler complains :
"Call to function 'strcpy' with no prototype"
#include <string.h> is required at the top of the module.
*/
function1(hc);
/* [HS] Compiler complains :
"Call to function 'function1' with no prototype"
A valid prototype for 'function1' is required at the top
of the module. You could also implement the function before
you use it. There is no obligation for main() to be the first
function of the module. IMO, the best place for main is the
last.
*/
/* [HS] Compiler complains :
"Function should return a value"
main is implicitaly declared with an int return. You need
to return something. You'd better use this form for main :
int main(void)
{
return 0;
}
*/
Quote:
}
void function1 (struct httpd_con* hc) { /* [HS] 'struct' added */
/* [HS] Compiler complains :
"Type mismatch in redeclaration of 'function1'"
*/
function2(hc);
/* [HS] Compiler complains :
"Call to function 'function2' with no prototype"
*/
Quote:
}
void function2 (struct httpd_con* hc) { /* [HS] 'struct' added */
/* [HS] Compiler complains :
"Type mismatch in redeclaration of 'function2'"
*/
printf("%d\n", hc->bleh);
/* [HS] Compiler complains :
"Call to function 'printf' with no prototype"
#include <stdio.h> is required at the top of the module.
*/
Quote:
}
/*
With BC++ 3.1 (MS-DOS)
Compiling STRUCT.C:
Warning STRUCT.C 16: Possible use of 'hc' before definition
Warning STRUCT.C 22: Call to function 'strcpy' with no prototype
Warning STRUCT.C 28: Call to function 'function1' with no prototype
Warning STRUCT.C 43: Function should return a value
Error STRUCT.C 45: Type mismatch in redeclaration of 'function1'
Warning STRUCT.C 49: Call to function 'function2' with no prototype
Error STRUCT.C 55: Type mismatch in redeclaration of 'function2'
Warning STRUCT.C 59: Call to function 'printf' with no prototype
*/
My last advice : "Always compile with the maximum level of warnings"
--
HS