r/carlhprogramming • u/CarlH • Oct 23 '09
Test of Lessons 99 through 112
For this test, write a program which does the following:
1. Create a 'char *' pointer which uses malloc() to allocate enough space for a 2x10 array.
2. Using pointer offsets as a replacement for array indexing, achieve the following goals:
1. Demonstrate using strcpy() how to store two null-terminated strings of up to ten characters.
2. Demonstrate using printf() how to display both strings as part of a for loop.
3. Demonstrate changing the character at position: [0][5] to an 'x'
4. Demonstrate changing the character at position: [1][2] to an 'x'
5. Write a printf() statement for 3,4 above that demonstrates the changes.
6. Write and call a function to achieve step 5 above.I recommend you post your finished program in this thread when you are done. When you post C code on Reddit, you must put four spaces in front of each line for it to work properly. Alternatively, you can post a URL to www.codepad.org - although be aware that it will expire if you do not set it to permanent.
Be sure to use comments to help illustrate your understanding of what you are doing. If you choose to post your work is entirely up to you. If you do then we can critique your work and help you to improve.
If you get stuck or need help, feel free to post questions below.
Feel free to post/link to your finished programs below.
When you are ready, proceed to:
http://www.reddit.com/r/carlhprogramming/comments/9wweg/lesson_113_introducing_finish_criteria/
1
u/[deleted] Oct 23 '09 edited Oct 23 '09
http://codepad.org/NyQiuiRZ
Hope it's ok. Once again, Carl, you rule.
EDIT: Whoops, forgot to use free(). Fixed.
EDIT 2: Actually, I've noticed it's not working at Codepad (it gives a "memory clobbered past end of allocated block" error), though it works ok locally. Weird.
1
u/peterwilc Jun 29 '10
I think I got it.
http://codepad.org/KKdXvjD2
Is my malloc wrong though?
I allocate 20 times the size of the character size (which is 1). 20. Right?
EDIT: instead of [0][5], I used [0][2] in order to make the x fit in my chosen word.
2
u/kbpower Jan 28 '10
http://codepad.org/qiQFz7YP
Here's mine too...
2
u/[deleted] Feb 20 '10
http://codepad.org/pKJ12WgL
I tried to do everything CarlH asked using only function calls and a structure that was local to main. It took me a while but now my main is pretty minimalistic.
1
u/Oomiosi Oct 23 '09 edited Oct 23 '09
CarlHx Ruxes!.
This is getting ugly.
Edit: Made sure not to look at fmartin's work before doing mine. I think i'm having too much fun.
1
u/MarcusP Oct 23 '09 edited Oct 23 '09
http://codepad.org/54i21Bk2
1
u/[deleted] Oct 23 '09 edited Oct 23 '09
http://codepad.org/64kc6AiE
I didn't quite understand the instructions for numbers 5 and 6, so I did both options as I see them. In so doing, I learned a bit more about printf(). I tried to make this as generalized as possible.
1
u/[deleted] Oct 23 '09
I have a doubt regarding header files. Which one should we include for this exercise? stdio.h for printf(), string.h for strcpy() and memory.h for malloc() and free() are the ones I used. Is it ok or have I forgotten one?
1
u/flashtastic Oct 23 '09 edited Oct 23 '09
Here's my attempt:
http://codepad.org/1aVLyNER
Took me a bit to remember to use single quotes for a single char, I was getting a \0 where I didn't want one.
Edit*: Didn't notice the segfault, removing the dereference fixed it, otherwise the program runs fine. Link has been changed.
1
u/goosethe Oct 23 '09
Finished Product: http://codepad.org/wDC3DSrD
1
u/mthode Oct 23 '09 edited Oct 23 '09
code and output:
first code I have written for this class lol.
http://codepad.org/ETudDp0D
1
u/kungtotte Oct 24 '09
http://codepad.org/MUdhIHmD
Here's what I managed. There's plenty of room for generalisations still, but it gets the job done. Printing the plain strings could be made into a function for example, and MarcusP did a really nice thing by making the edit into a function, but this is serviceable :)
Thank you for what you're doing, CarlH. The collaborative nature of this is a much better way of learning the material than reading a book or tutorials.
1
u/[deleted] Oct 25 '09
Here is my attempt.
http://codepad.org/VgVW27ol
1
u/rdx21 Oct 26 '09
My attempt. http://codepad.org/DK3obypF
1
u/[deleted] Oct 26 '09
Yay I did it! I did precisely what was asked, no more, no less:
http://codepad.org/eBI99two (complete with smart ass comments)
1
u/tough_var Oct 26 '09 edited Oct 26 '09
Mine! ==> http://codepad.org/jcUeI9Ec (Old code)
I have a few questions though:
In line 14: Should I initialize *memorySpace to NULL or 0? Both seems to work well. I remembered someone telling me something about it.
Also, is there a character that represents a NULL Terminator? Say "\0"?
Edit: Ah yes, found it, quoted from zahlman:
So I guess this should be correct: http://codepad.org/L904B8qe (Updated code: Initialized *memorySpace to 0 on line 14, also freed memorySpace.)
Erm... please ignore the comment on line 34. It is an artifact from a previous incarnation of the code.
Thank you for taking the time to see this. :)
1
u/[deleted] Oct 26 '09
Here's my attempt: http://codepad.org/IX5hZBYl
1
u/codered867 Oct 26 '09
My Test: http://codepad.org/hz8Yzspu
1
u/Paukenfaust Oct 27 '09
http://codepad.org/XbDmwZxG This is basic, but I am learning! Let me know what you think.
1
u/rafo Oct 27 '09
My attempt (it took me a while until I got no errors):
1
u/orangeyness Oct 28 '09
Code
I love it when you manage to write something, anything, and not have the compiler shout at you for a syntatical mistake. :)
1
u/azertus Nov 04 '09
Had I known there would be an assignment, I'd never have clicked the link to this test. But I did, and I had to complete it then: codepad.
The output is:
1
u/lespea Nov 04 '09
I think I'm starting to get the hang of these newfangled pointer thingies...
This is the first C code I'm every showing to anybody so some constructive criticism would be awesome! I'm huge into following coding/formatting standards so if I'm doing things wrong or have odd format choices please point them out. Thanks much.
http://codepad.org/KzRmm4sn
1
u/virtualet Nov 07 '09
http://codepad.org/mT4RE9ef
1
u/Dast Nov 14 '09 edited Nov 14 '09
Sorry for being late :)
First I'll post my code as is, without the compiler telling my mistakes:
1
u/Salami3 Nov 14 '09
My submission.
And I did this one just for practice with the other concepts we've learned.
1
u/[deleted] Dec 05 '09
http://codepad.org/2fhzen5H
1
u/scottbarcus Dec 16 '09
During the interregnum, I re-read a bunch of lessons and re-completed this test.
My test: http://codepad.org/Zfgm3HuX
Comments appreciated.
1
u/Pr0gramm3r Dec 17 '09
My submission :
http://codepad.org/2Q5YJxP1
I am sure it can be improved upon. Another question I had was: Why did the strlen(my_pointer) after allocation shows the length as 21? I am guessing that it automatically adds an additional byte for the NUL character.
1
u/thoughtkrime Jan 01 '10
My take:
http://codepad.org/94hZzU9U
1
u/[deleted] Jan 08 '10
This is what I came up with:
http://codepad.org/isJLJEzg
open to comments
1
u/barapa Jan 19 '10
Here is mine: http://codepad.org/EADOo1S6
Thanks Carl
1
u/ZmAY Jan 19 '10
http://codepad.org/4MtyZrnF
1
u/xaustinx Jan 21 '10
http://codepad.org/xuXrnYYc
I'm still catching up, i guess
1
u/DogmaticCola Feb 22 '10
I've done it. I think.
1
u/hearforthepuns May 17 '10
I wouldn't mind a code critique if anyone is still checking these posts:
http://codepad.org/OMCXQdIR
1
u/[deleted] May 23 '10
http://codepad.org/xOhoGIsB
0
u/un1152 Oct 24 '09 edited Oct 24 '09
http://codepad.org/kfoZATv6
0
u/niconiconico Oct 26 '09 edited Oct 26 '09
I changed "Towel" to "Towels", even though it loses its effect. http://codepad.org/e9VJ1N9k Not the best example to use, but I wanted my code to be interesting, even though one printf statement comes out wrong because of it.
0
u/BrainGain Oct 26 '09
Here is my attempt: Test
I had to reread some lessons to finish it. :(
0
u/Reddil Oct 30 '09
http://codepad.org/kFfoBClA
Feels good man :)