2

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.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
mousey
  • 11,601
  • 16
  • 52
  • 59

9 Answers9

9
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.

R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
6

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.

Skilldrick
  • 69,215
  • 34
  • 177
  • 229
  • I am familiar with all the functions in c standard library. But I often get tricked by c pointers and functions that need special attention. – mousey Jul 16 '10 at 19:58
  • words of wisdom, +1 for Kernighan & Ritchie @mousey: so your problem is not really the standard library but rather some problems with exact understanding of pointers. Then K & R is the best choice – Kris Jul 16 '10 at 20:36
  • @mousey: But pointers in C++ (which you say you are familiar with) are identical to pointers in C; so what is the real problem? – Clifford Jul 17 '10 at 08:46
  • +1: I did not mention K&R specifically in my answer because you had already done so. As the "accepted" answer though it addresses only one part of the question. As already commented they should have been posted separately for this reason. – Clifford Jul 17 '10 at 08:51
1

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.

eruciform
  • 7,680
  • 1
  • 35
  • 47
  • Nitpick: The array declaration does not create a `char *` pointer called `c`. It has an address, but it is implicit. A pointer may be created from it---sometimes implicitly, as when the array "degrades" into a pointer---depending on how it is used. `c` in this case *is* the array, not a pointer to it. – Tim Schaeffer Jul 16 '10 at 22:31
  • @tim - how would you suggest i rephrase that to be accurate without being too confusing. also, are you sure that the choice to have a memory location for the address of the pointer to `c` isn't a compiler-dependent thing? – eruciform Jul 16 '10 at 23:35
  • @eruciform: it is hard to explain without being confusing, I admit. C could have had a special operator to give you a char pointer which points to the first char of `c`, but it's easier to just let the array be used as a pointer. That `c` is an array and not a pointer is not compiler-dependent because this fact is not a fact of the resulting program, but a fact of the abstract machine that the `C` language works in. – Tim Schaeffer Jul 17 '10 at 03:18
  • @tim - i'll add a comment then trying to explain this... there. i played it halfway - once based on how it seems, and then a footnote to explain in depth... hopefully between the two, people can make some sense of it. – eruciform Jul 17 '10 at 03:29
  • @eruciform: Cool. I know I said it was a nitpick, but it does have practical effects. E.g. `&c` gives you different things (of different types) when `c` is a pointer than when `c` is an array. – Tim Schaeffer Jul 17 '10 at 18:27
  • @tim: yes, that is true, they aren't completely cast-able into each other. though i suppose you could claim that it makes a(n) `int * const c`, or at very least an equivalently-typed token within the compiling phase, and maybe linking phase... this is definitely programmatic navel-gazing at its best. :-) – eruciform Jul 17 '10 at 18:44
1

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.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
0

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

sas4740
  • 4,510
  • 8
  • 26
  • 23
0

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:

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

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.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Disagreed. C is a much simpler language. If you're going to suggest an alternative newbie language, pick a high level one with garbage collection. – nmichaels Jul 16 '10 at 21:03
  • I am sorry for mentioning this thing. I am good at C++ and Visual studio. I am using C++ for the past 3 years. But I am getting myself into kernel programming. I just wanted a useful quick start guide to Linux kernel debugging.So I asked about gdb. – mousey Jul 16 '10 at 22:33
  • @Nathon: C is simpler, but you do not have to use *all* of C++ to use it effectively. Even for straight C code C++ compilation has benefits. Apart from that, it seems he is not a newbie, and for kernel programming, such languages are not appropriate (although conceded it was not previously clear that was the status) – Clifford Jul 17 '10 at 08:37
  • @mousey:If you already know C++, then you already know C. The compiler will soon reject any C++ specific code you use. What debugger did you use with C++ (since GDB can be used for C++ too)? Given Torvalds' renowned hatred of C++, it *is* probably C that you need in this case, but C++ is just as suited to systems level programming as C. – Clifford Jul 17 '10 at 08:44
  • @mousey: For kernel level work you may want to consider using GDB remotely over a TCP/IP connection. If you do not have a separate target and host machine you can use a virtual machine target and develop and debug on a single machine. Note that it is looking more and more like you should have separated out these questions. – Clifford Jul 17 '10 at 11:20
0

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.

Rei Miyasaka
  • 7,007
  • 6
  • 42
  • 69
0

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.

0

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

kwytay
  • 141
  • 1
  • 1
  • 6