ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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:
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.
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.
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.