![]() |
|
#1
|
|||
|
|||
Help on passing in arrays in functions?Hi,
I''m new to C and dunno why the full char array buffer doesn't get passed into method. Run the following program, type "prompt bash" as input. you'll see that prompt gets split up into "pro"...sth. This is because the size of buffer is only 4 instead of 256. Thanks. P.S : i hope the code works, coz i'm takin snippets from my file. CPP / C++ / C Code:
|
|
#2
|
||||
|
||||
|
Hi nustu:
One major problem that I see is your use of fgets: Try putting this check into your program somewhere: CPP / C++ / C Code:
I bet you didn't get the result you wanted. Sizeof is simply going to return the sizeof your variable in bytes, (probably 4). So when you use: CPP / C++ / C Code:
It will only read in the first 4 charecters of your input. You need to specify the real size. Alot of times, I will use a #define for this like CPP / C++ / C Code:
Then in the fgets: CPP / C++ / C Code:
There are some other things that I am not 100% sure on either. Unfortunately, your code did not compile as is, mostly due to no definition of seps. HTH, d |
|
#3
|
|||
|
|||
|
Thanks, dsmith. I got it. The reason why it outputs 4 is because 4 bytes is used for a pointer.
|
|
#4
|
||||
|
||||
|
Quote:
Yeah, you were on the right track to figuring it out when I replied. I notice that you changed your original post a bit, before I could reply, so I think you would have figured this out pretty quick. A quick note as well, strtok is not well thought of by some. Here is a quote from the man page of strtok Quote:
This may not be a big deal for you, but you may want to keep this limitations in mind. Cheers, d |
|
#5
|
|||
|
|||
how to pass thishow do we pass two-dimentional integer array into functions and also how to return a 2-d integer array??
i pass a 1-d array by reference like this: CPP / C++ / C Code:
plz guide... (am i a novice?? OH HELL YEA..!!) ![]() |
|
#6
|
||||
|
||||
|
Quote:
It is actually quite a bit different. Here is an example that passes a 2-d array by reference: CPP / C++ / C Code:
|
|
#7
|
||||
|
||||
|
Arrays are ALWAYS passed by reference (value). You do not need to make a pointer to an array. You can simply pass it like this:
CPP / C++ / C Code:
__________________
-Aaron |
|
#8
|
||||
|
||||
|
You are absolutely correct Aaron. I missed the forest for the trees again. My sample still holds, but the comments in it are misleading.
Actually, I rarely use arrays in anything that I do. Pointers are much easier to use and understand IMHO and are dynamic. Thanks again, aaron |
|
#9
|
||||
|
||||
|
Oh, yeah. The code you posted was fine. I just wanted to clarify it for you so he could understand it a little better.
__________________
-Aaron |
|
#10
|
|||
|
|||
got it..thx a LOT for the quick response guys...
if i keep on getting advise like this from u, i wish there will be some day when i will be advising u on something..!! HEHE.. amen! thx a lot again.. it might be an easy thing for u, not for me though. |
Recent GIDBlog
Toyota - 2008 August Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The