Thread: Suggestions for improving.

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    Suggestions for improving.

    Hello everyone ,
    I took a break from c for a while. Now I started to study again. I was stuck last time. My book's c part is nearly done. And I want to practice my c codes. I am not so advanced. But I need you to tell me some standart c programmes which will help me improve while I am trying to deal with them. I need some suggestions. Thank you very much.


    Ah also I relaized that I could ask a question. I think I know the answer but you know sometimes you want to be fully sure about it with all your mind.
    Here it is :

    It is said that the name of string is the pointer pointing the first component of the array. But in fact it is not truly a pointer , is it? Because we gotta put & and [0] in order to point the first component. Sometimes I get confused because of this.

    I will also ask something about pointers. Even a pointer which doesnt show anything, spends some bytes from the memory , doesnt it? I mean when we assign a variable to the pointer it is just like assigning a number to a integer variable ??

    I will be glad if you answer my simple questions. Thank you again.
    Last edited by ozumsafa; 09-05-2007 at 08:21 AM.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I'm a bit confused as to what you actually want... Suggestions on a programme to write to get back into C? Suggestions on how to write better code?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Not really those , I need standart simple programs which are written by anyone who tries to learn c , (like snake the game) .

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So one common thing to do to learn string (and file) handling is to write program that takes a text-file as input and reports how many times each word occurs in the text.

    That's a fairly easy task, yet complicated enough to give you a challenge, I think.

    As to your confusion about strings and such... Arrays and pointers are sort of interchangeable in C, but not ALWAYS, and not ENTIRELY.

    char str[] = "Somestring"; // Create an array of char str, which contains 'Somestring\0'
    char *pstr = str; // Pointer to string - pstr contains the ADDRESS of str.

    &str[0] and pstr have the same value (some address). str[0] and *pstr have the same value ('S') str[8] and *pstr+8 have the same value ('n'). Note that (*pstr)+8 is not the same thing, that adds 8 to the letter 'S'.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I did that program you have told about before. Anything else that you can reccomend?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try writing it again, with your current knowledge! It should be real easy. Then post the result, and we'll discuss.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    59
    If you want you are more than free to write my assignement lol

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I also wanna ask :

    char *str;
    gets(str);
    printf("%s",str);

    gives error when compiling.
    But :

    char str[20];
    gets(str);
    printf("%s",str);

    does not give error. So doesnt it prove that pointers and arrays are different. Because we dont need do assign something to an array . It is also a vary as NULL .But pointers have to point something?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Hmm. There's no error compiling on my machine, with this:
    Code:
    #include <stdio.h>
    
    int main() {
      char *s;
    
      gets(s);
      return 0;
    }
    However, it will crash, almost certainly, since *s is pointing at some random place in memory, and storing data in a random place is never a good idea.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But I do agree that arrays and pointers aren't the same thing. An array IS some memory, a pointer is a variable that holds an address of some memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    On my compiler , It says s is never assigned a value.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You mean
    Code:
    gets.c:4: warning: 's' might be used uninitialized in this function
    Yes, I had to enable optimization to get that (as the "where is a variable used tracing" is not enabled when you don't have optimization on). And it's entirely valid - it isn't assigned a value, so the code wouldn't work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not to be pedantic or anything, but why use gets() anyway? http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dwks View Post
    Not to be pedantic or anything, but why use gets() anyway? http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    As you probably can see from my previus posts, I'm normally pointing out the dangers of gets(), in this case, I didn't think it was meaningfull, as the entire bit of code isn't particularly meaningfull.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, I know. That's why I was being pedantic by pointing it out.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions and question about bool pointers
    By Akkernight in forum C++ Programming
    Replies: 15
    Last Post: 03-22-2009, 12:27 PM
  2. Free Book Suggestions!
    By valaris in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-08-2008, 10:25 AM
  3. Hi, Suggestions on my program please..
    By Siggy in forum C++ Programming
    Replies: 44
    Last Post: 11-23-2004, 10:55 PM
  4. Need Suggestions
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2003, 05:46 AM
  5. Suggestions.
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 02-06-2002, 12:21 PM