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.

8
  • 4
    allot of questions here, try re-posting them as separate questions. Also try searching for each sub-question. I'm sure searching this site will give you some good links to tutorials. Commented Jul 16, 2010 at 19:43
  • 4
    Your question title does not at all describe your question. It would be wise to edit and rephrase it to have actual meaning. Commented Jul 16, 2010 at 19:47
  • 1
    The question about char* versus char[] has been asked and answered many times. Here is one of them: stackoverflow.com/questions/1880573/… Commented Jul 16, 2010 at 19:50
  • 1
    @mousey: Ask the first two questions separately. You'll get answers then. Commented Jul 16, 2010 at 20:06
  • 1
    For books on C, see stackoverflow.com/questions/562303/…. Commented Jul 16, 2010 at 20:28

9 Answers 9

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

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.
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
@mousey: But pointers in C++ (which you say you are familiar with) are identical to pointers in C; so what is the real problem?
+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.
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.

1 Comment

+1 comp.lang.c is totally awesome, especially when it comes to subtleties.
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.

6 Comments

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

Comments

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.

5 Comments

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.
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.
@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)
@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.
@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.
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.

Comments

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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.