Structure pointers, char, and passing 
Author Message
 Structure pointers, char, and passing

Hi,

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
I try accessing a structure pointer, I get a bad memory address
failure.  The same happens when I try printing that var.  Here is some
sample code of what I'm trying to do (shortened).

typedef struct httpd_con {
        int bleh;
        char bah[200];

Quote:
}

main () {
        http_con* hc;  
        hc->bleh = 1;
        strcpy(hc->bah, "hi!");
        function1(hc);

Quote:
}

void function1 (httpd_con* hc) {
        function2(hc);

Quote:
}

void function2 (httpd_con* hc) {
        printf("%d\n", hc->bleh);

Quote:
}

In the real code, the examples are spread accross several source files.
By the time function2 is invoked, bleh and bah seem to have magically
disappeared, and trying to print them causes a segfault (gdb tells me
that the memory address is non-existant when trying a 'print').  I know
I'm doing something wrong...but what?

Yann

--

--------------------------------------------------------------------

Atrus Trivalie Productions      www.redshift.com/~yramin
                                irm.it.montereyhigh.com
Monterey High IT                www.montereyhigh.com
ICQ                             46805627
Marina, CA      
--------------------------------------------------------------------



Wed, 06 Mar 2002 03:00:00 GMT  
 Structure pointers, char, and passing

Quote:

> main () {
>         http_con* hc;
>         hc->bleh = 1;
>         strcpy(hc->bah, "hi!");
>         function1(hc);
> }
        ...
> By the time function2 is invoked, bleh and bah seem to have magically
> disappeared, and trying to print them causes a segfault (gdb tells me
> that the memory address is non-existant when trying a 'print').  I
> know
> I'm doing something wrong...but what?

There's a problem on the second line (I did not look any farther).  You
are dereferencing hc and setting one of the members it points to, but
you have not initialized hc to point to anything; its value is undefined
(usually garbage).  Initialize hc to point to something reasonable and
the problem will go away.  A good compiler will warn you about this
behavior.

Also, main should be explicitly declared to return int and there should
be a return statement at the end of its block.

--

 Alcyone Systems | irc maxxon (efnet) | web http://www.alcyone.com/max/
    San Jose, CA | languages en, eo | icbm 37 20 07 N 121 53 38 W
             USA | Sat 1999 Sep 18 (29%/949) | &tSftDotIotE
 __
/  \ Think twice before you speak to a friend in need.
\__/ Ambrose Bierce



Wed, 06 Mar 2002 03:00:00 GMT  
 Structure pointers, char, and passing

Quote:

> Hi,

> 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
> I try accessing a structure pointer, I get a bad memory address
> failure.  The same happens when I try printing that var.  Here is some
> sample code of what I'm trying to do (shortened).

> typedef struct httpd_con {
>         int bleh;
>         char bah[200];
> }

> main () {
>         http_con* hc;
>         hc->bleh = 1;
>         strcpy(hc->bah, "hi!");
>         function1(hc);
> }

> void function1 (httpd_con* hc) {
>         function2(hc);
> }

> void function2 (httpd_con* hc) {
>         printf("%d\n", hc->bleh);
> }

> In the real code, the examples are spread accross several source files.
> By the time function2 is invoked, bleh and bah seem to have magically
> disappeared, and trying to print them causes a segfault (gdb tells me
> that the memory address is non-existant when trying a 'print').  I know
> I'm doing something wrong...but what?

#include <stdio.h>
#include <string.h>

typedef struct httpd_con {
        int bleh;
        char bah[200];

Quote:
} h_con;

void function1 (h_con* hc);
void function2 (h_con* hc);

main () {
  h_con  an_address_for_hc;
        h_con* hc;

  hc = &an_address_for_hc;
        hc->bleh = 1;
        strcpy(hc->bah, "hi!");
        function1(hc);

  return 0;

Quote:
}

void function1 (h_con* hc) {
        function2(hc);

Quote:
}

void function2 (h_con* hc) {
        printf("%d\n", hc->bleh);
  printf("%s\n", hc->bah);

- Show quoted text -

Quote:
}



Thu, 07 Mar 2002 03:00:00 GMT  
 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



Thu, 07 Mar 2002 03:00:00 GMT  
 Structure pointers, char, and passing
Sorry about that :)  I was just putting up a little sample code
illustrating what I was trying to do.  The real code is very long ATM.

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>
> >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];
> }; /* [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;
>    }
> */
> }

> 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"
> */
> }

> 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.
> */
> }

> /*
> 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

--

--------------------------------------------------------------------

Atrus Trivalie Productions      www.redshift.com/~yramin
                                irm.it.montereyhigh.com
Monterey High IT                www.montereyhigh.com
ICQ                             46805627
Marina, CA      
--------------------------------------------------------------------



Thu, 07 Mar 2002 03:00:00 GMT  
 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
>I try accessing a structure pointer, I get a bad memory address
>failure.  The same happens when I try printing that var.  Here is some
>sample code of what I'm trying to do (shortened).

>typedef struct httpd_con {
>        int bleh;
>        char bah[200];
>}

You never complete the 'typedef' with the type's new name.  Nor does it
end with a semicolon.  (No semicolon means that it would be the return
type for the next function, or an error otherwise.)

Quote:
>main () {

"main" needs the 'int' return type.  It may have a screwed up return type
due to your previous errors, though.

Quote:
>        http_con* hc;  

"http_con" is an non-existing type since you never completed the
'typedef.'  It may also be a misspelling since you use "httpd_con" for the
type's name later on.

A bigger problem is that "hc" is never initialized, so all the following
lines are using a wild pointer.  Just take away the star in the
delclaration, or add a line malloc'ing space for the object.

Quote:
>        hc->bleh = 1;
>        strcpy(hc->bah, "hi!");
>        function1(hc);
>}

>void function1 (httpd_con* hc) {

"httpd_con" is an non-existant type since you never completed the
'typedef.'  (C++ would let you get away with it, though.)  This function
should have a prototype preceeding the "main" function.

Quote:
>        function2(hc);
>}

>void function2 (httpd_con* hc) {

"httpd_con" is an non-existant type since you never completed the
'typedef.'  (C++ would let you get away with it, though.)  This function
should have a prototype preceeding the "function1" function.

Quote:
>        printf("%d\n", hc->bleh);
>}

>In the real code, the examples are spread accross several source files.
>By the time function2 is invoked, bleh and bah seem to have magically
>disappeared, and trying to print them causes a segfault (gdb tells me
>that the memory address is non-existant when trying a 'print').  I know
>I'm doing something wrong...but what?

--
Daryle Walker
Video Game, Mac, and Internet Junkie
walke751 AT concentric DOT net


Thu, 07 Mar 2002 03:00:00 GMT  
 Structure pointers, char, and passing

Quote:

> Sorry about that :)  I was just putting up a little sample code
> illustrating what I was trying to do.  The real code is very long ATM.

So how are we supposed to know what are your 'little problems' and what
are your typos?  Thanks for merely posting a sample, if you were being
considerate of our time, but please actually compile whatever you post
and fix the errors you understand.  'Homer' is great, but he shouldn't
have to provide a compiler-message service.  And who knows?  --You may
find your problem yourself once you've eliminated all the "distracting"
code.

...Cheers!

--

==========================================

==========================================



Thu, 07 Mar 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. structure containing pointer to char over the network

2. Comparison with pointer to pointer to char, and char

3. How to include char arrays of pointers in structures

4. how to pass an array of char pointers as an argument

5. Passing char pointer problem

6. Passing an Array of pointers to char

7. A question about passing a char pointer...

8. Passing Char Pointers Between Threads

9. pointer to structure passed by ref

10. newbie pointer to structure passing.

11. Passing structure pointers as arguments

12. Passing a structure pointer

 

 
Powered by phpBB® Forum Software