Thread: What does this code means??

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    49

    What does this code means??

    Hi all,
    i have a number of code that i don't really understand them, can anyone please interprate them for me. Below are just a single line of code in a program.

    Code
    =========
    int n;
    Dict dictionary;
    char *buffer;
    temp = 0;
    int unsigned wi = 0;

    dict = DictInit(n);
    word[char_count++] = character;
    word[char_count] = '\0';
    DictAddWord(dictionary, word);
    buffer = DictWord(dictionary, wi);
    temp = strlen(buffer);
    buffer[temp-1] != '\n';

    =============
    code end


    Thanks in advance.
    diana --> programming is tough

  2. #2
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    put code tags around your code to make it readable. See this thread :
    http://cboard.cprogramming.com/showthread.php?t=25765

    and maybe you can tell us what you DONT understand about this code so we can explain it , or else our replies might yield even more questions
    Last edited by Brain Cell; 10-11-2004 at 07:50 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  3. #3
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    This doesn't look like a single line of code. At an initial glance, it seems to add words to a dictionary.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    What i want to know is the explanation of the code above,

    For example:

    Code:
    a = b + c;
    The above code say that "a" will store the value after "b" + "c".
    or if it is a pointer, what value will the pointer holds and where does is points to,

    Hope that my queries will be clearer this time round.

    regards
    diana --> programming is tough

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    considering the code above is invalid and won't compile it does nothing.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Thantos
    considering the code above is invalid and won't compile it does nothing.
    As I understand it she posted several selected codelines from a program. I don't think she wants to know what the program does but what the lines do, i.e:

    int n; defines a variable named "n" of the "int" type, a type to store integer numbers.

    char *buffer; defines a variable "buffer" of the type "char *", which means a pointer to a [or usually more] units of type char. A pointer usually "points" at a physical location in memory. You will somehow need to reserve memory for that character(s) and then assign the address of that memory to the pointer before you can fully use it. To get the data the pointer points at, you will have to dereference it, i.e. char mychar= *buffer;

    I think you should search for a good tutorial or even better, a beginners book. Programmers tend to explain stuff more complicated than might be.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yes but the code they supplied is invalid due to at least one variable misnaming.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    As I understand it she posted several selected codelines from a program. I don't think she wants to know what the program does but what the lines do, i.e:

    int n; defines a variable named "n" of the "int" type, a type to store integer numbers.

    char *buffer; defines a variable "buffer" of the type "char *", which means a pointer to a [or usually more] units of type char. A pointer usually "points" at a physical location in memory. You will somehow need to reserve memory for that character(s) and then assign the address of that memory to the pointer before you can fully use it. To get the data the pointer points at, you will have to dereference it, i.e. char mychar= *buffer;
    Nyda is right, all the code are just lines of code in a program, i just want to know, what are the lines of code mean.
    Last edited by dianazheng; 10-11-2004 at 05:16 PM.
    diana --> programming is tough

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Step 1: Buy a good book on C.
    Step 2: Read it.
    Step 3: ... ?
    Step 3: Profit!

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    int n; //Declares a variable of type integer called n

    Dict dictionary; //Declares a variable of type Dict called dictionary

    char *buffer; //Declares a pointer to a character called buffer

    temp = 0; //Sets temp equal to 0

    int unsigned wi = 0; //Declares a variable of type integer called wi. Integers usually hold values from -Something (I forget the actual number) to +Something. unsigned means to ignore all the negative values, start counting from zero, and therefore, now you can hold twice as many positive numbers.

    dict = DictInit(n); //DictInit has been defined somewhere. This line initializes dict with whatever DictInit returns, when passed the variable n.

    word[char_count++] = character; //This sets position "char_count" of the array "word" equal to whatever character is equal to. the ++ then increments the value of char_count by 1. (I believe the order of that is correct...any corrections?)

    word[char_count] = '\0'; //Now that char_count has been increased by one, it sets position "char_count" in the word array equal to '\0' (which denotes the end of a string)

    DictAddWord(dictionary, word); //DictAddWord is a defined function somewhere, I'm assuming this adds the value of "word" to your variable (probably a structure?) dictionary.

    buffer = DictWord(dictionary, wi); //This calls a defined function named DictWord and passes parameters "dictionary", your Dict type, and wi (no clue what that does), but whatever the function returns, buffer is now equal to it. (I'm guessing that it returns the word at position wi from the dictionary structure, and sets buffer equal to it).

    temp = strlen(buffer); //A variable named temp, which I'm unsure of the type (probably integer) gets set equal to the length of your character array (buffer)

    buffer[temp-1] != '\n'; //This checks that the second last position (temp-1) in your array "buffer" is not a newline character '\n' (Most likely used in an If Statement?)

    [EDIT]
    Note: I doubt though you'll be able to find too many people very often that will go through code line-by-line and explain what each step does. I happen to have lots of free time at the moment.

    As others said, a good idea would be to search Google (best search engine for C++) for tutorials. There are many out there that will guide you from the basics, with fully explained examples that will help you understand everything in the future, as opposed to just what you need at the moment.

    I.e. Learning the basics of Functions, Input Parameters, and Return will not only allow you to understand what's happening when something like "DictWord" appears, but also when any other type of function appears in the future, you'll be able to quickly understand what is going on.

    Don't be afraid to ask questions, just be sure to ask in an organized way, reading the FAQ and Posting Guidelines is recommended (trust me, people will be happier to help if you follow the guidelines), and try to do research (Google, or searching the forum) beforehand so that you can narrow down your questions to specifics.

    Best of luck with your project there, again, if you have questions, somebody is usually around to answer. Welcome to the boards.
    [/EDIT]
    Last edited by Epo; 10-11-2004 at 07:42 PM.

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    Thanks Epo, i get it.
    diana --> programming is tough

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    This thread reminds me a bit of HelpMePlease's debut.

  13. #13
    Registered User
    Join Date
    Oct 2004
    Posts
    49
    Code:
    fread(&character, sizeof(char), 1, inf);
    1 more line of code, i couldn't find the meaning to this line of code, anyone pls help me to interprate them.

    thanks
    diana --> programming is tough

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
    DESCRIPTION
    The function fread reads nmemb elements of data, each size bytes long,
    from the stream pointed to by stream, storing them at the location
    given by ptr.

    RETURN VALUE
    fread and fwrite return the number of items successfully read or writ-
    ten (i.e., not the number of characters). If an error occurs, or the
    end-of-file is reached, the return value is a short item count (or
    zero).

    fread does not distinguish between end-of-file and error, and callers
    must use feof(3) and ferror(3) to determine which occurred.
    You should learn to love man pages. There's not much about a function that they won't teach you.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM