Thread: struct question

  1. #31
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Elysia View Post
    Need pointer lessons? Here you go:
    http://cpwiki.sourceforge.net/A_pointer_on_pointers
    It might enlighten you a little. Pointers are just variables that contains a virtual address, nothing more, nothing less. It does not allocate memory.
    Try to avoid misinforming when attempting to inform; it will serve a greater purpose.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  2. #32
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by -EquinoX- View Post
    well I already declare line:

    char* line = NULL;

    the thing is that when I remove the HashTableInsert, it kind of works
    And do you think the NULL pointer points at a character array?
    And for that matter, you're using &line, which means that the string is being put into the memory address of line (not where line points!), but you've only got room for maybe three letters there, not to mention you won't ever get them back out, since how do you plan to reinterpret line as a char instead of a char*?

  3. #33
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can't store any information in an ordinary char* pointer that isn't pointing at allocated memory. You should either use an array, or dynamically allocate some memory for the pointer to point to.

    I haven't read the rest of this thread, but since you have free(), I'm guessing you want to use the dynamic memory approach. Try something like this
    Code:
    char *line = malloc(256);
    [edit] It seems a few posts appeared while I was typing this one up. [/edit]
    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.

  4. #34
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    You're doing wrong again. Twice, in fact.
    Code:
          char* line = NULL;
          size_t line_length;
          int chars_read = getline(&line, &line_length, fr);
    Did you read the link I gave you? It should give insight as to why this fails.
    If you DID read it and didn't understand something, then ask.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #35
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    doesn't actually get line fills the line for us?

  6. #36
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by -EquinoX- View Post
    doesn't actually get line fills the line for us?
    Yes, but you have to have somewhere to put it. It will not allocate memory for you.

  7. #37
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, getline() should fill in the pointer for you.

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

  8. #38
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    Yes, but you have to have somewhere to put it. It will not allocate memory for you.
    If we assume the example here is a working one, then it will:
    http://linux.die.net/man/3/getline

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

  9. #39
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    yeah..matsp.. thanks for pointing that out.. that's the getline what I mean

  10. #40
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by matsp View Post
    If we assume the example here is a working one, then it will:
    http://linux.die.net/man/3/getline

    --
    Mats
    Hey what do you know. How many versions of getline does that make, three I think? The C++ one doesn't work that way, does it? (All my docs say it needs room at the pointer, but who knows.)

  11. #41
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    sorry guys, I should have mentioned it to you before... so why do I keep get a seg fault even though that's right?

  12. #42
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    So we'll keep reading:
    Code:
    if (line[0] != '\t'){
    	  if (rule.target != NULL){
    	    rule.dependency = dep;
    	    rule.buildcmd = bld;
    	    rule.depSize = depIndex + 1;
    	    rule.bldSize = bldIndex + 1;
    	    HashTableInsert(t, rule.target, &rule);
    	    RuleDestruct(&rule);
    	  }
    	  char* pch = strtok(line, ":");
          	  int h;
             for (h = 0; pch != NULL; h++){
    	    if (h == 0){
    	     rule.target = pch;
                 printf("Target %s\n", pch); 
    	     bldIndex = 0;
    	     depIndex = 0;
    	    }
    1. When we get here, I don't see how rule.target is set.
    2. In your for-loop, I don't see how pch is going to change. (You don't change it inside the loop.) Edit: I lost a brace -- so found where pch changes; never mind.

  13. #43
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay rule.target is set? what do you mean? how can I set that?

    and it seems that I am missing a free(line) here, but don't know where to put..

    my point here is actually just to create a temporary rule which I can put into the HashTable.. do I have to do a malloc first? if yes then how much space do I have to malloc?
    Last edited by -EquinoX-; 04-14-2008 at 03:15 PM.

  14. #44
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by -EquinoX- View Post
    okay rule.target is set? what do you mean? how can I set that?
    Well, I don't know. Reading the code, this is the first time I ever saw rule.target, and right away we're comparing it to something. Who knows what value is stored in there? I'm not sure what's going on here -- it appears that you're deleting the old rule maybe; but there's no guarantee that rule.target is NULL the first time you get to this point. So it's quite possible you're following garbage pointers here.

  15. #45
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay fixed that already, next

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 03:59 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM