Aslai

Untitled

Nov 25th, 2012
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.26 KB | None | 0 0
  1. [9:58:27 PM] David: K got that in
  2. [9:58:29 PM] David: now what
  3. [9:58:37 PM] Andrew Story: int main( int argc, char* argv[] )
  4. {
  5.  
  6. }
  7. [9:58:42 PM] Andrew Story: That's "Proper"
  8. [9:58:55 PM] David: Proper formatting as well?
  9. [9:59:04 PM] Andrew Story: Well kinda, yea
  10. [9:59:04 PM] David: Explain argc and argv
  11. [9:59:08 PM] David: argv is an array
  12. [9:59:13 PM] David: of chars
  13. [9:59:15 PM] David: what does * do
  14. [9:59:20 PM] Andrew Story: argc is the number of arguments on the commandline
  15. [9:59:26 PM] David: arg Count
  16. [9:59:27 PM] David: ahaa.
  17. [9:59:30 PM] David: bitch
  18. [9:59:36 PM] Andrew Story: argv is an array of pointers to char types
  19. [9:59:50 PM] David: Ah right * means pointer
  20. [9:59:54 PM] Andrew Story: In C, a string or array is a pointer to a block of contiguous memory
  21. [9:59:59 PM] David: so argcount and argvars?
  22. [10:00:05 PM] Andrew Story: or argvalues
  23. [10:00:10 PM] David: Argvalues right
  24. [10:00:12 PM] Andrew Story: whatever makes sense to you
  25. [10:00:20 PM] David: I get pointers already if that helps
  26. [10:00:26 PM] David: It's like passing by reference
  27. [10:00:27 PM] Andrew Story: That's a HUGE help
  28. [10:00:36 PM] David: ByRef vs ByVal
  29. [10:00:37 PM] Andrew Story: That's what catches most people with C
  30. [10:00:46 PM] David: Alrighty well I'm good to go there already :)
  31. [10:00:55 PM] Andrew Story: Also in C, strings end with a NULL (o)
  32. [10:00:57 PM] Andrew Story: 0
  33. [10:01:11 PM] David: Right, null terminated
  34. [10:01:16 PM] David: knew that as well
  35. [10:01:21 PM] Andrew Story: so in memory, it's literally stored as "blah blah blah\0"
  36. [10:01:23 PM] David: don't know how that translates to code but
  37. [10:01:24 PM] David: Ah
  38. [10:01:35 PM] Andrew Story: To get the length, you have to count all the characters =p
  39. [10:01:55 PM] David: I'm assuming that takes seconds to write a method for though
  40. [10:02:02 PM] Andrew Story: There's strlen(char*) which does it for you, but is obviously not a O(1) function
  41. [10:02:07 PM] David: Right
  42. [10:02:27 PM] Andrew Story: So we may as well print hello world, right?
  43. [10:02:55 PM] Andrew Story: In the main function, type
  44. puts("HELLO ME");
  45. return 0;
  46. [10:03:20 PM] David: Puts = put string
  47. [10:03:24 PM] Andrew Story: yep
  48. [10:03:27 PM] David: I'm assuming that is to the console then
  49. [10:03:30 PM] Andrew Story: Yea
  50. [10:03:34 PM] David: Okay, easy enough
  51. [10:03:43 PM] David: How to run?
  52. [10:03:45 PM] Andrew Story: Puts also inserts a carriage return and a newline at the end
  53. [10:03:58 PM] David: So s.o.pln
  54. [10:04:07 PM] Andrew Story: F9 or the gear with the green triangle under it
  55. [10:04:10 PM] David: It's a printline
  56. [10:04:11 PM] David: kk
  57. [10:04:12 PM] Andrew Story: http://puu.sh/1u2cj
  58. [10:04:22 PM] David: How do I print sans crlf
  59. [10:04:37 PM] David: It ran as expected with the proper output.
  60. [10:05:02 PM] Andrew Story: printf(char*,...) will print a formatted string with no CRLF
  61. [10:05:29 PM] David: I am familiar with printf in other languages
  62. [10:05:33 PM] Andrew Story: So for a simple string like Hello World, you can just do printf("Hello World")
  63. [10:05:55 PM] Andrew Story: http://www.cplusplus.com/reference/cstdio/printf/
  64. [10:05:57 PM] David: The other arguments which are optional and unlimited are replacements yes?
  65. [10:06:03 PM] David: yep
  66. [10:06:04 PM] Andrew Story: That has a list of all the flags and what they do
  67. [10:06:30 PM] Andrew Story: Basically, if you know the syntax of C and have access to that site, that's all you really need
  68. [10:06:50 PM] Andrew Story: Just get really familiar with cstdio and that's all you really need to know
  69. [10:06:56 PM] David: Teach me the syntax
  70. [10:07:37 PM] David: How do I print anything which is not a string
  71. [10:07:44 PM] David: must it be strictly cast to a string?
  72. [10:07:47 PM] Andrew Story: With printf
  73. [10:07:55 PM] Andrew Story: printf("%i", inthere);
  74. [10:08:00 PM] Andrew Story: That prints an int
  75. [10:08:16 PM] Andrew Story: printf("%f",floathere); prints a float
  76. [10:08:29 PM] Andrew Story: %s for string, %c for char, etc
  77. [10:08:31 PM] David: Right right got that
  78. [10:08:48 PM] Andrew Story: Well, there are just a few fundamental types really
  79. [10:08:50 PM] David: Okay, should I hit a loop of some kind, how to I terminate the program?
  80. [10:08:59 PM] David: Or any other failure
  81. [10:09:12 PM] Andrew Story: The program will terminate on one of two conditions
  82. [10:09:28 PM] Andrew Story: Either the main function ends, or exit(int) is called
  83. [10:09:40 PM] Andrew Story: exit(int) is the same as return int; from main
  84. [10:09:52 PM] Andrew Story: however exit(int) can be called anywhere in the program
  85. [10:10:00 PM] David: Right
  86. [10:10:01 PM] David: Okay well
  87. [10:10:12 PM] David: What if I accidentally an infinite loop
  88. [10:10:21 PM] David: ctrl alt del kill process?
  89. [10:10:29 PM] Andrew Story: Just hit x on the cmd window
  90. [10:10:37 PM] David: WHAT IF THERE IS NO CMD WINDOW
  91. [10:10:48 PM] Andrew Story: THEN CTRL ALT DEL THE PROCESS
  92. [10:11:02 PM] Andrew Story: OR ADD AN OUT INSIDE OF THE LOOP
  93. [10:11:08 PM] David: OKAY SOUNDS GOOD YO
  94. [10:11:25 PM] Andrew Story: So loops are pretty much the same as anywhere else
  95. [10:11:43 PM] Andrew Story: You have label/goto, while, for, do/while
  96. [10:12:04 PM] Andrew Story: You can make labels with labelname:
  97. [10:12:14 PM] Andrew Story: goto labelname;
  98. [10:12:15 PM] David: So that's more assembly there
  99. [10:12:21 PM] Andrew Story: Yes
  100. [10:12:22 PM] David: but gotos are evil of course
  101. [10:12:23 PM] Andrew Story: Never use goto
  102. [10:12:26 PM] Andrew Story: =p
  103. [10:12:29 PM] David: because it fux wit da scope
  104. [10:12:34 PM] Andrew Story: exactly
  105. [10:12:43 PM | Edited 10:12:45 PM] Andrew Story: and can cause memory leaks
  106. [10:12:47 PM] David: mhm
  107. [10:13:02 PM] Andrew Story: while(condition!=0){}
  108. [10:13:13 PM] Andrew Story: in C, any integer can be used as a boolean
  109. [10:13:16 PM] David: for(int i =0; i < 1000; i++){} valid?
  110. [10:13:25 PM] Andrew Story: if it's 0, it's regarded as false, otherwise true
  111. [10:13:35 PM] David: So no true/false singletons?
  112. [10:13:38 PM] Andrew Story: Yes, but only in c99 and later
  113. [10:13:46 PM | Edited 10:13:50 PM] Andrew Story: There's no true or false in C
  114. [10:13:52 PM] David: kk
  115. [10:14:03 PM] David: i could define em :P
  116. [10:14:08 PM | Edited 10:14:11 PM] Andrew Story: You can always #define true 1 and #define false 0 though
  117. [10:14:18 PM] David: mhm
  118. [10:14:25 PM] David: [10:13 PM] David:
  119.  
  120. <<< for(int i =0; i < 1000; i++){} valid?
  121. [10:14:32 PM] Andrew Story: [10:13 PM] Andrew Story:
  122.  
  123. <<< Yes, but only in c99 and later
  124. [10:14:59 PM] Andrew Story: In codeblocks, you can enable c99 by going to project->build options
  125. [10:15:14 PM] David: Lets not touch that yet
  126. [10:15:18 PM] David: what's proper syntax then
  127. [10:15:19 PM] Andrew Story: Alright
  128. [10:15:39 PM] Andrew Story: int i; for( i = 0; i < 1000; i++ ){}
  129. [10:16:00 PM] Andrew Story: in c99, you can put the type declaration in the scope of the for
  130. [10:16:19 PM] David: Ah so the only thing different is the i declaration
  131. [10:16:24 PM] Andrew Story: yea
  132. [10:16:52 PM] David: code ran as expected
  133. [10:16:56 PM] Andrew Story: You can also do for( i=0, j=0, k=1; expression; runseveryloop )
  134. [10:16:56 PM] David:
  135. int main( int argc, char* argv[] )
  136. {
  137. int f = 1 + 1;
  138. printf("%i", f);
  139. int i;
  140. for(i = 0; i < 100; i++){
  141. printf("%i", i);
  142. }
  143. return 0;
  144. }
  145. [10:17:06 PM] David: Yep that is standard as well
  146. [10:17:37 PM] Andrew Story: in codegolf I try to pack my entire for statement in to runseveryloop
  147. [10:17:41 PM] Andrew Story: =p
  148. [10:17:47 PM] David: Everyone does ;0
  149. [10:18:27 PM] David: Okay so
  150. [10:18:36 PM] Andrew Story: Someone pointed out to me yesterday that -~a is a+1
  151. [10:18:37 PM] David: What else do I need to know
  152. [10:18:42 PM] David: Haha cool
  153. [10:18:43 PM] Andrew Story: and -~-~-~a is a+3
  154. [10:18:46 PM] Andrew Story: main(a,b,c){scanf("%i",&b);a=b;a=a<0?a:-a;for(c=0;a<~1;a=-~-~-~a)c=-~c;b=b<0?-c:c;}
  155. [10:19:00 PM] Andrew Story: That takes a number and divides it by 3 without arithmetic =p
  156. [10:19:14 PM] David: Totally cheating
  157. [10:19:15 PM] Andrew Story: if() is pretty standard
  158. [10:19:44 PM] Andrew Story: I also have an answer that doesn't use unary negate =p
  159. [10:19:45 PM] Andrew Story: I(unsigned a){a=a&1?I(a>>1)<<1:a|1;}main(a,b,c){scanf("%i",&b);a=b;a=a<0?a:I(~a);for(c=0;a<~1;a=I(I(I(a))))c=I(c);b=b<0?I(~c):c;}
  160. [10:20:03 PM] David: if(0){}elseif(0){}else{} <- valid?
  161. [10:20:09 PM] David: should run last else
  162. [10:20:21 PM] David: mandatory space between else and if?
  163. [10:20:36 PM | Edited 10:20:41 PM] Andrew Story: if(expr){}else if(expr){}else{}
  164. [10:20:46 PM] David: K so space.
  165. [10:20:48 PM] David: Now
  166. [10:20:49 PM] David: IO
  167. [10:20:52 PM] Andrew Story: Keep in mind, {} is a code block
  168. [10:20:55 PM] David: Teach me console IO
  169. [10:20:58 PM] David: yes of course
  170. [10:21:18 PM] David: Also whats this malloc shit I keep hearing about
  171. [10:21:21 PM] Andrew Story: In any application other than functions and structs, codeblock markers are OPTIONAL if it's only one line
  172. [10:21:33 PM] David: Right like any other language
  173. [10:21:45 PM] Andrew Story: so you can if(expr) foo() else bar()
  174. [10:21:53 PM] Andrew Story: elses stick to the nearest if
  175. [10:23:07 PM] Andrew Story: so in
  176. if(expr1){
  177. if(expr2)
  178. if(expr3)
  179. Junk
  180. else
  181. otherjunkifnotexpr3
  182. } else otherjunkifnotexpr1
  183. [10:23:33 PM] David: That other else is for the if(expr2)
  184. [10:23:35 PM] David: right?
  185. [10:23:47 PM] Andrew Story: There's a code block specifier
  186. [10:24:03 PM] David: Aha.
  187. [10:24:09 PM] David: So therefore expr1
  188. [10:24:12 PM] David: right
  189. [10:24:13 PM] Andrew Story: expr2 is in the code block so the last else corresponds to expr1
  190. [10:24:27 PM] Andrew Story: That's caught a few people up
  191. [10:24:36 PM] Andrew Story: So you want IO?
  192. [10:24:38 PM] David: Yea
  193. [10:24:45 PM] David: Console IO and malloc...
  194. [10:24:59 PM] Andrew Story: Let's start with IO
  195. [10:24:59 PM] David: Wait
  196. [10:25:05 PM] David: You do know this is C and not C++ right?
  197. [10:25:09 PM] Andrew Story: Yes
  198. [10:25:09 PM] David: Just double checking
  199. [10:25:19 PM] David: I feel like I should have allocated some memory somewhere by now O_O
  200. [10:25:26 PM] Andrew Story: C++ standard IO is dumb and I never use it
  201. [10:25:34 PM] David: Good cause this is C
  202. [10:25:35 PM] David: :P
  203. [10:25:37 PM] Andrew Story: So
  204. [10:25:49 PM] Andrew Story: Basic, dumb input is gets(char*)
  205. [10:26:09 PM] Andrew Story: that will read user input up to a newline character and stick it in to your char* buffer
  206. [10:26:21 PM] David: explain my char* buffer
  207. [10:26:27 PM] Andrew Story: usage:
  208. char buffer[1000]; gets(buffer);
  209. [10:26:45 PM] Andrew Story: Ok
  210. [10:26:54 PM] Andrew Story: There are a few fundamental types
  211. [10:26:56 PM] David: Okay I see
  212. [10:26:59 PM] David: Makes sense
  213. [10:27:13 PM] David: So because chars and strings are by reference we can pass like that
  214. [10:27:45 PM] Andrew Story: functions, structs, ints/shorts/chars, floats/doubles, and pointers to any of those types, pointers to any of those pointers, etc
  215. [10:27:54 PM] Andrew Story: Then there's the super special type, void
  216. [10:28:31 PM] Andrew Story: The size of a void is always one memory unit, however you cannot use void as a type to hold values
  217. [10:28:39 PM] David: Uh
  218. [10:28:40 PM] David: why
  219. [10:28:42 PM] David: whats the point
  220. [10:29:01 PM] Andrew Story: You can only use void to specify the returning of nothing, or to specify a generic pointer value
  221. [10:29:09 PM] David: K
  222. [10:29:20 PM] Andrew Story: The point is that void* is a very neutral pointer type to use
  223. [10:29:47 PM] Andrew Story: anything dealing with memory pass void pointers around
  224. [10:29:59 PM] David: Okay
  225. [10:30:02 PM] David: now we get to memory
  226. [10:30:13 PM] David: How do we deal with memory
  227. [10:30:23 PM] Andrew Story: There are a few modifiers that you can use on the int family and a few for the float family
  228. [10:30:39 PM] Andrew Story: ints can be made short, long, unsigned, and register
  229. [10:30:57 PM] Andrew Story: register types try to be kept in CPU memory for fast manipulation
  230. [10:31:11 PM] Andrew Story: long and short should be obvious
  231. [10:31:25 PM] Andrew Story: (un)signed specifies whether or not to make it signed
  232. [10:31:36 PM] Andrew Story: You can also use short and long on floats and doubles
  233. [10:31:42 PM] Andrew Story: So memory
  234. [10:31:51 PM] Andrew Story: There's two ways to allocate memory
  235. [10:32:00 PM] Andrew Story: Either on the stack or on the heap
  236. [10:32:17 PM] David: I understand a stack and I understand a heap but I do not understand them in this context
  237. [10:32:33 PM] Andrew Story: allocating memory on the stack is called static allocation since the size is fixed at compile time
  238. [10:32:53 PM] Andrew Story: I think C99 has arrays that can change size on the stack, but I've never used them
  239. [10:33:27 PM] Andrew Story: The benefit of static allocation is that when the function ends, all the memory is automatically released
  240. [10:33:52 PM] Andrew Story: This also means that you can't return a reference to statically allocated memory that was allocated within the function
  241. [10:34:15 PM] Andrew Story: So
  242. char ret[1000];
  243. return ret;
  244. IS A BIG NO NO
  245. [10:34:24 PM] David: Roight
  246. [10:34:29 PM] David: Ahhhh
  247. [10:34:34 PM] David: so we'd have to malloc for that
  248. [10:34:37 PM] Andrew Story: Yea
  249. [10:34:40 PM] David: so ret could be used elsewhere
  250. [10:34:49 PM] David: cheeky bastard
  251. [10:35:03 PM] Andrew Story: Also I'd like to point out that arrays are implicitly references
  252. [10:35:10 PM] David: Yes you mentioned I think
  253. [10:35:16 PM] Andrew Story: so char ret[] is a char* underneath
  254. [10:35:28 PM] David: Quick question
  255. [10:35:29 PM] Andrew Story: char ret[][] is a char** underneath, etc
  256. [10:35:33 PM] David: What is a struct?
  257. [10:35:44 PM] Andrew Story: A collection of variables
  258. [10:36:10 PM] Andrew Story: Think of it like a class but without any object oriented qualities applied to it
  259. [10:36:47 PM] Andrew Story: So it can have any variables that it wants in it, it can also house pointers to functions, etc
  260. [10:37:00 PM] David: So like an interface you can poke.
  261. [10:37:07 PM] Andrew Story: Kinda, yea
  262. [10:37:08 PM] David: For lack of a better term
  263. [10:37:11 PM] David: Alright makes sense
  264. [10:37:25 PM] Andrew Story: They're useful for organizing data
  265. [10:37:36 PM] David: They're classes without the classes
  266. [10:37:40 PM] Andrew Story: yep
  267. [10:37:55 PM] Andrew Story: So malloc
  268. [10:37:59 PM] David: Mallocccc
  269. [10:38:33 PM] Andrew Story: malloc(size_t) will return a void pointer to a block of memory of size_t (unsigned integer) length, or a null pointer on failure
  270. [10:38:54 PM] Andrew Story: This is heap allocation, or dynamic allocation
  271. [10:39:06 PM] Andrew Story: The allocation lives for as long as the program or until it's freed
  272. [10:39:43 PM] Andrew Story: It's VERY important to keep track of all mallocs and make sure they're freed when you're done
  273. [10:39:47 PM] David: Yes
  274. [10:39:54 PM] David: Otherwise you could BSOD yourself
  275. [10:40:06 PM] Andrew Story: Well, not since windows 95 =p
  276. [10:40:11 PM] David: :P
  277. [10:40:14 PM] David: Okay so wait
  278. [10:40:21 PM] Andrew Story: Now the program will just likely crash if you run out of memory
  279. [10:40:31 PM] David: going back to the char ret[1000]; return ret; quandary
  280. [10:40:56 PM] David: how do I then apply the memory I want to allocate to the ret array
  281. [10:41:02 PM] Andrew Story: you could do char *ret = malloc( 1000 * sizeof(char) ); return ret;
  282. [10:41:17 PM] David: aha
  283. [10:41:20 PM] Andrew Story: sizeof(value) returns the size of a type in memory units
  284. [10:41:34 PM] David: And it just knows to make that an array?
  285. [10:41:44 PM] David: or is that how arrays are stored anyway?
  286. [10:41:46 PM] David: Oh
  287. [10:41:47 PM] David: Duh
  288. [10:41:48 PM] Andrew Story: sizeof() is a compile time operation
  289. [10:41:49 PM] David: yeah
  290. [10:41:53 PM] David: arrays are stored that way anyway
  291. [10:42:00 PM] Andrew Story: arrays are just pointers to a block of memory :)
  292. [10:42:02 PM] David: Yeah yeah
  293. [10:42:44 PM] Andrew Story: C conventions are kind of silly
  294. [10:42:55 PM] Andrew Story: When specifying a pointer, you use * to specify such
  295. [10:43:06 PM] Andrew Story: When you want to dereference the pointer, you use *
  296. [10:43:09 PM] David: Why do you need it for when you malloc but not when you []
  297. [10:43:14 PM] David: Explain dereference
  298. [10:43:26 PM] Andrew Story: you have pointer a with value 1000;
  299. [10:43:38 PM] Andrew Story: say it's an int pointer
  300. [10:43:38 PM] David: OHHHH WAIT
  301. [10:43:43 PM] David: IT GETS THE VALUE BACK OUT
  302. [10:43:49 PM] Andrew Story: You dereference a and get the int stored at 1000
  303. [10:43:54 PM] David: RIGHT RIGHT K
  304. [10:43:57 PM] David: makes sense
  305. [10:44:00 PM] Andrew Story: You do that via *a
  306. [10:44:09 PM] David: So * before a variable is deref
  307. [10:44:13 PM] Andrew Story: yep
  308. [10:44:15 PM] David: * after a data type is pointer
  309. [10:44:28 PM] David: and you must dereference the array to create one in memory using malloc
  310. [10:44:29 PM] Andrew Story: Kind of
  311. [10:44:45 PM] Andrew Story: When specifying multiple variables, you need a * for each pointer
  312. [10:44:51 PM | Edited 10:45:08 PM] Andrew Story: So int *a, *b, c, d;
  313. [10:45:01 PM] Andrew Story: a and b are int pointers, c and d are ints
  314. [10:45:29 PM] Andrew Story: [10:44 PM] David:
  315.  
  316. <<< and you must dereference the array to create one in memory using malloc
  317. [10:45:36 PM] Andrew Story: Err no
  318. [10:45:47 PM] Andrew Story: Malloc returns the address at which it allocated memory
  319. [10:46:06 PM] Andrew Story: So you store that in to an address variable (pointer)
  320. [10:46:10 PM] David: Ohh
  321. [10:46:22 PM] Andrew Story: you can then dereference the pointer
  322. [10:46:51 PM] Andrew Story: so in the case of any pointer, you can use arrayorpointer[index]
  323. [10:47:03 PM] Andrew Story: that's the equivalent of *(arrayorpointer+index)
  324. [10:47:18 PM] Andrew Story: In C, there's "pointer arithmatic"
  325. [10:47:34 PM] Andrew Story: so pointer+1 isn't pointers address plus one
  326. [10:47:54 PM] Andrew Story: It's pointers address plus the size of the pointed to type times one
  327. [10:48:04 PM] David: I
  328. [10:48:06 PM] David: am confused
  329. [10:48:14 PM] David: I think I learned enough about C for the evening
  330. [10:48:19 PM] Andrew Story: so with a 32 bit int, then it moves the address forward by 4 bytes
  331. [10:48:29 PM] David: Heck, this is probably 60x more what most will know entering the class
  332. [10:48:37 PM] David: but I'll be back tomorrow for more lessons for sure
  333. [10:48:40 PM] Andrew Story: =p
  334. [10:48:42 PM] Andrew Story: alright
  335. [10:48:47 PM] Andrew Story: Have a good night sleep
Advertisement
Add Comment
Please, Sign In to add comment