![]() |
|
#1
|
||||
|
||||
getting string size at run timeHuy.
I am trying to write a simple program to get the size of two strings at run time. I mean I let the user enter the strings and then I wanna use the sizeof operator to compare the sizes of the two strings. Heres my code: CPP / C++ / C Code:
and it gives me the following errors: Compiling... strng_copy.c D:\VC++\chap7_pointers\str_copy\strng_copy.c(5) : error C2133: 'string1' : unknown size D:\VC++\chap7_pointers\str_copy\strng_copy.c(5) : error C2133: 'string2' : unknown size D:\VC++\chap7_pointers\str_copy\strng_copy.c(11) : warning C4034: sizeof returns 0 D:\VC++\chap7_pointers\str_copy\strng_copy.c(12) : warning C4034: sizeof returns 0 Error executing cl.exe. strng_copy.obj - 2 error(s), 2 warning(s) How does one go about telling the compiler that the size(s) would be known only at run time and not at compile time? Is there any other way to go about it? Hoping to hear from you guys Best Regards, Aijaz Baig. Last edited by cable_guy_67 : 07-Jul-2006 at 02:58.
Reason: Please surround your C code with [c] ... [/c] codetags
|
||||
|
#2
|
||||
|
||||
Re: getting string size at run timeQuote:
You may want to read this, too. As someone brand new to the forum, maybe you can help us. We have a post with RED background and YELLOW text that new people seem to not see or understand. Is there something we can change that would indicate to someone new that the information should be read before posting a question? The reason I ask is that the moderators have requested 4 times that to use code tags, and the Guidelines explain how. You obviously haven't read them yet... __________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#3
|
||||
|
||||
Re: getting string size at run timeHi.
Thnks for showing me the link that proved to be immensely helpful. Additionally I clearly don't understand what you wrote after that. I mean how does that pertain to my post? Are u asking me for my opinion on that Or is it that you want me to format the way I enter my messages so as to make them more readable for others. By the way where is the post with the red background and yellow text? Kindly elaborate as I am currently very confused about what u meant Best Regards, Aijaz Baig. |
|
#4
|
|||
|
|||
Re: getting string size at run time@aijazbaig1
YELLOW text w/ RED background is the post title @ top of the list: http://www.gidforums.com/f-41.html @WaltP Since you asked, as one also a new member here, I suggest adding "BEFORE POSTING," in front of the "Read This" . The "Read This >>> Guidelines for posting requests for help -" could be: "BEFORE POSTING, Please Read This Post! " The additional info "Guidelines ...blah.", as helpful as it might be to some, will actually cause others NOT to read it. Some people think they already know the rules so will ignore it. (ithink the red&yellow colors & bold are good but I cannot repo that here...) Also, in the Guidelines text body, perhaps a short example(shorter than this!) of what the tagindicator is and why it is might help. E.g. one with the tagindicator: [postStart] What's the error in this code: Code:
versus one without the tagindicator [postStart] What's the error in this code: Code:
versus one with only the tagindicator [postStart] What's the error in this code: CPP / C++ / C Code:
OF COURSE you will want to use a shorter example. That just happened to be some that I was looking at recently... The KEY part seems to be that you need to state: "REPLACE the "CODE" word in the code block tag with the appropriate language tag: C/C++/CPP, JAVA, CSS, etc... " Because that is what you really want, correct? Part " 1.A.d. " lists the code types but apparently c, c++, and cpp are all the same so the statement " using c, c++ or cpp are all the same " might be worthwhile. Also, that seems to be missing XML and related... Is there no parser for XML and associated types? What about PERL or other languages(BASH, etc...)? IOW, the list of language types should mention those that are not supported and what the poster should do when posting one of those other types of code. And, PHP and HTML already have code tags to be used so that info, in part 1.A.d., is not needed, right? That makes it confusing: ' does one do what Walt needs or use the built-in tags? ' If you do want people to use the built-in tags, then say such; if not then state that too. As you might know, people tend to take the shortest route possible which means putting an extra, non-clickable tag requires effort and forethought. Some might forget the tagindicator too since preoccupation with the problem is quite likely. So, perhaps modify the bar to add that to the "tagindicator" code block as part/one of the clickable elements. The HTML and PHP widgets are there so C, C++, XML, etc... could be there too. A drop-down selection box would be nice... I'm sure part of the problem is that people figure '' I am posting in the "X" subforum so the code is, of course, "X" '' and they also generally mention that in the text description so they might feel it is redundant info and not understand that it is required to be able to use this forum. In general, instructions have to be very, very simple and to the point. When learning something new, most everyone is just like a young child. I don't have a problem with using apropos tags ... just hope I don't forget. c-ya PS: There really is an error in that code according to the ch interpreter... |
|
#5
|
||||
|
||||
Re: getting string size at run timeQuote:
Response has been moved to Misc Programming. Thanks, fhj52 __________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#6
|
||||
|
||||
Re: getting string size at run timeHello..
I have come up with the following code which uses a char pointer to allocate memory at run time on the heap. It then uses these allocated memory locations to store the characters sequentially. CPP / C++ / C Code:
Is there an even more efficient way in which we can allocate memory for each single character each time before reading a character at that location? And even if we manage to do that..won't it make the program more slower as we have to call the malloc function every time for each single character? What do you people think would be a clever thing to do..allocate space for 500 characters at once or allocate space for 1 char at a time but use as many calls to malloc as there are characters to be stored?? And I hope this time I have used the proper tag indicators Best regards, Aijaz. |
|
#7
|
||||
|
||||
Re: getting string size at run timeWhy are you making this so complicated?
Define a char* buffer of 500 Input using fgets() as described in the link I posted. And you never free()ed the buffer you malloc()ed -- a no-no. __________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#8
|
||||
|
||||
Re: getting string size at run timeHi.
Well, as said by walt, i tried the following code which uses fgets and it did work. And by defining a char * buffer of 500, did u mean allocate space for 500 characters using malloc? Nevertheless, heres my code and it does work CPP / C++ / C Code:
I hope I got it correct on everything..however if you think it could be further improvised, then let me know..my ears are always open to advices on making further improvisations |
|
#9
|
||||
|
||||
Re: getting string size at run timeQuote:
Define a buffer: char buf[500]; not Allocate a buffer: malloc (500); There is no reason to allocate anything. People use allocation way too much -- simply because they can is not a good reason. Quote:
And to be nit-picky -- improved is the word, not improvised -- coding should rarely, if ever, be improvised... ;-) __________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
|
#10
|
||||
|
||||
Re: getting string size at run timeHello.
@waltP Quote:
What did u mean by the sentence above. Did u mean my usage of malloc was inappropriate as in my syntax wasn't exactly what malloc expects? Or did u mean I should remove the malloc and instead define a buffer explicitly? I hope that u meant the latter... and yeah, my english won't ever be like yours Best Regards, Aijaz Baig. |
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| String pad. | pjpav | Java Forum | 6 | 13-Nov-2005 12:10 |
| Help wit my source code compiler errors | Krandygrl00 | C++ Forum | 1 | 06-Jun-2005 08:14 |
| runtime, compile time... | kai85 | C Programming Language | 2 | 19-May-2005 22:05 |
| Having a problem | Chuckles | Computer Hardware Forum | 19 | 13-Sep-2004 12:17 |
| Display Size and maybe download time | BobbyDouglas | MySQL / PHP Forum | 5 | 08-Nov-2003 00:18 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The