LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-15-2004, 07:33 AM   #1
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Rep: Reputation: 15
structs & scope


I was wondering if anyone could give me some advice of good programming practice about passing objects in C, from scope to scope.... lets say I have a struct, that I want to send as an parameter in a function, i would do the following:

Code:
typedef struct {
char *str;
int i;
} tempObj

//Function prototype: void some_func(tempObj *obj)
tempObj temp_obj;
some_func(&temp_obj);
Now I can access the object in my function, as a pointer...
I'll try to assign something:

Code:
void some_func(tempObj *obj)
{
obj->i = 10;
obj->str = malloc(10);
strcpy(obj->str, "something");
printf("\ni = %d\nstr = %s", obj->i, obj->str);
}
1 :Now I have assigned something so my object, and everything works fine. What if I want to call another function with this functions' scope. How till the functoin call look like`? Like this?

Code:
//Function prototype: void another_func(tempObj *obj)
//Call within some_func() function
...
printf("\ni = %d\nstr = %s", obj->i, obj->str);
another_func(obj);
2: if this is right, can I use the object in another_func() the same way, I could in some_func() scope? I'll illustrate:

Code:
void another_func(tempObj *obj)
{
obj->i = 11;
strcpy(obj->str, "IDoNotKnow");
printf("\ni = %d\nstr = %s", obj->i, obj->str);
}
3: Now the last question. If I go all the way back to main scope, can I access the info with the following syntax?

Code:
...
some_func(&temp_obj);
if(obj.i == 11)
printf("\ni = %d\nStr = %s", obj.i, obj.str);
I'm having problems with the sending the pointer to an object further in scope, and I'm just trying to learn the proper way to do this. Any help will be greatly appriciated.

Regards Jnusa

Last edited by jnusa; 11-15-2004 at 07:36 AM.
 
Old 11-15-2004, 08:23 AM   #2
yuray
Member
 
Registered: Apr 2003
Location: Russia, Khotkovo
Distribution: Debian
Posts: 146

Rep: Reputation: 15
1. you still must put address of obj
another_func(&obj)
2. yes
3. yes

If you want ask again, put the full code (or small indissoluble part with bug)
 
Old 11-15-2004, 08:27 AM   #3
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
Hmm

1: I have to put the address of the pointer? For the question 1, I call another function within a function. So I'm sending a pointer as a parameter. Do I need to send the address of this pointer, like you say? &obj

Yeah I know it's easier to debug the code directly, but I'm not at that stage, where I want to give up. Just wanted to be sure, of what I was doing, and if I understand you right, I've been doing at least one thing wrong.
 
Old 11-15-2004, 07:17 PM   #4
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
I think yuray misunderstood your question. You don't need to use another_func(&obj) if you are inside some_func, only another_func(obj).

The reason is, inside some_func, obj is already of type "tempObj*", so it is exactly what another_func needs. Passing the address would give you a type "tempObj**" instead.
 
Old 11-16-2004, 01:16 AM   #5
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
Yeah, I tried testing the test code, and figured that I just had to send the pointer. So I guess I'm handeling the objects (or structs.... done more cpp than c) right. Guess I'm doing something else wrong then. Sometimes Í have problems with allocating mem for a pointer within a struct. And basic int variables within a struct, can be corrupted, when I get out of function scope. I always send the struct as a pointer parameter, to avoid returning the struct... I'm not to happy about returning struct, because I have no way of making a copyconstructor like in cpp (or is there). What would be considered good programming practice? Sending the object as a parameter or/and returning it?
 
Old 11-16-2004, 01:43 AM   #6
yuray
Member
 
Registered: Apr 2003
Location: Russia, Khotkovo
Distribution: Debian
Posts: 146

Rep: Reputation: 15
Im sorry for misunderstanding.
Working example. Whats you problem ?

#include <stdio.h>
typedef struct {
char *str;
int i;
} tempObj;

void another_func(tempObj *obj)
{
obj->i = 11;
strcpy(obj->str, "IDoNotKnow");
printf("\ni = %d\nstr = %s", obj->i, obj->str);
}

void some_func(tempObj *obj)
{
obj->i = 10;
obj->str = (char *)malloc(10);
strcpy(obj->str, "something");
printf("\ni = %d\nstr = %s", obj->i, obj->str);
another_func(obj);
}

int main (int argc, char **argv)
{
tempObj temp_obj;
some_func(&temp_obj);
return(0);
}
 
Old 11-16-2004, 05:33 AM   #7
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
Well I was trying to avoid posting any any code, because the program itself is pretty big. I'll try to isolate the case.

I'm trying to make my own http 1.1 compliant library and I'm having great diffeculty reading all the data. It seems, when the message body has a size of e.g. 8568 it will only read 2680 or some other fraction of the content. I first thought that maybe I wasn't allocating right, but it seems ok. Also tried with static buffer... same problem.

I'll post some code a little later...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copy structs in c alaios Programming 10 09-10-2005 02:31 PM
pointers to structs in C spuzzzzzzz Programming 5 06-03-2004 05:41 PM
static structs? simbo Programming 3 02-05-2004 04:00 AM
Self referential structs in C? MadCactus Programming 14 01-28-2004 05:29 PM
structs and allocation of mem h/w Programming 4 12-22-2003 12:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration