开发者

Need some help with C programming [closed]

开发者 https://www.devze.com 2023-01-07 21:25 出处:网络
As it c开发者_运维问答urrently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely so
As it c开发者_运维问答urrently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 10 years ago.

I am new to C programming and I am having problems understanding common pitfalls and common usages of different library functions in C programming. Can some one point me to a good resource where I can learn subtleties in C programming. Also can some one point me to a good resource learn debugging tools like gdb.

Also I want to know what is the difference between char *c="hello"; and char c[10]="hello" . Can some one tell me which one is recommended over the other in different situations.

Thanks & Regards,

Mousey.


char *c = "hello";

That makes c a pointer and is pointing to memory that should not be modified (so you cannot modify the data). But since c is a pointer, you can change where it points to.

char c[10] = "hello";

That makes c an array and arranges to have the array initialized with the specified string. Since it's an array, you can modify the data (although make sure you don't overflow the buffer) but you cannot change where in memory c references.


Just read The C Programming Language and write code. If you're new to it then you need first-hand experience so you can learn what the subtleties are. Just reading a list won't help a huge amount.


For the language itself, the book by the language designers is a good read. Be sure to do the exercises.

Another useful resource is the comp.lang.c FAQ. You've asked question 6.2 (be sure to read 6.1 and 6.3 as well).

It's explained in the links above, but just to insist: pointers and arrays are not the same thing in C. Rather, there are circumstances where the language requires a pointer, but you can use an array instead and it'll be converted automatically.


The difference is as follows:

char *c = "hello";

Created several things:

  • a char* called c
  • a static string in memory filled with "hello\0"
  • and it sets c to the address of that static memory

Whereas:

char c[10] = "hello";

Creates:

  • a char* called c (See note below)
  • 10 slots in memory someplace
  • sets c to the address of the first location in the above
  • and it treats "hello" like {'h','e','l','l','o','\0'}, thus copying those values into c[0] through c[5]
  • depending on the compiler, "hello" may or may not get allocated someplace in memory in addition

Note:

In the second case, there technically isn't both an array and a variable that exists just to contain the address of the array, it just seems that way. So c is really just an alias for the address of the first location in the array. Updated with info from Tim below in the comments.


For gdb, the docs are online http://sourceware.org/gdb/current/onlinedocs/gdb/

And a cheat sheet which I find much more useful: http://users.ece.utexas.edu/~adnan/gdb-refcard.pdf


My first recommendation would be that unless you have a really good reason to learn C specifically, learn C++ instead. I realise that is probably going to be contentious amongst some; just something to consider if you have not already done so.

For resources, in the first instance a good book is always best, but if you are looking for on-line resources then you will find that many are C++ related, some deal with C and C++; different styles of writing and presentation suit different users; try some of these:

  • http://www.cprogramming.com/
  • http://www.howstuffworks.com/c.htm
  • http://www2.its.strath.ac.uk/courses/c/
  • The C Book (online)
  • The C Book (HTML download)
  • The C Book (PDF)

The following C++ related sites include excellent coverage of the C standard library:

  • http://www.cplusplus.com/
  • http://www.cppreference.com/wiki/

With respect to GDB, I applaud the appreciation of the benefits of using a symbolic debugger, it is remarkable how many developers avoid this essential tool, but suggest that using raw GDB may put you off such tools for life. If you are able to use VC++ on Windows its debugger is second to none, and VC++ Express is free. If you must use GDB (because you are using Linux for example) I suggest that you use GDB integrated into an IDE such as Eclipse, or KDevelop, or use the stand-alone Insight debugger. If you do choose to be hardcore and use GDB directly, there seems to be few resources on how to use it effectively beyond the GDB manual itself. There is also Debugging with GDB: The GNU Source-Level Debugger at $30.


If you're mathematically inclined, Project Euler can probably give you some good practice in certain areas, especially in array manipulation and stuff.

But keep in mind, there's more to programming than math -- despite what your prof might tell you.


The "C Traps and Pitfalls" by Andrew Koenig is an excellent book precisely for learning about C pitfalls. It is a pretty thin book, though. The comp.lang.c FAQ someone else pointed to is also an excellent resource.


Try doing a search for "c programming puzzles" and you'll find lots of resources on the tricky subtleties of the language itself (and there are many). Eg. here

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号