Thread: Can I create a fileheader using printf?

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Subsonics View Post
    How come? If I create an array to store 5 chars is there another way of doing it? I did like this
    Code:
    char array[5];
    Yes, that allows 5 characters to be stored. I think tabstop was referring to your pointer variable, but I'm not sure.

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

  2. #17
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by matsp View Post
    But a string in C is a sequence of characters that end with a zero - so pointer to char will point to the FIRST of those characters. In the case of "var" in your code, it will be stored somewhere in the program's memory as a sequence of four characters, 'v', 'a', 'r', '\0' - where this is stored is entirely up to the compiler, but generally strings are (nowadays) stored in a read-only data part of your executable file, and loaded into memory just like the code and any pre-initialized data.

    Note that "char *var" in itself does not allow even ONE character to be stored. It is a place where you can store the ADDRESS of a character - when you initialize that string with "var", it is given the address that the compiler put those characters in.

    --
    Mats
    Ok, thanks for clarifying Mats.

    Whats your oppinion about the use of char pointers to point to a complete string? Is it considered kosher generally or is it best avoided?
    Last edited by Subsonics; 01-07-2009 at 04:06 PM.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Subsonics View Post
    Ok, thanks for clarifying Mats.

    Whats your oppinion about the use of char pointers to point to a complete string? Is it considered kosher generally or is it best avoided?
    Char pointers that point to constant strings is fine (generally speaking - however, for the structure you described in the initial post here, you DO need to have a char array). You should really use "const char *var = "Some string";" - that way the compiler will tell you if you try to modify such a string - which will most likely cause some sort of problem - strange results later on or a crash.

    Char pointers to variable content is more complex, as you need to somehow allocate memory, and both that and char arrays requires that you know the maximum length of the final content (and that you validate that you are not going beyond that maximum length, as that will store data into "something elses memory" which leads to corrupted data content and/or a crash - or if you are really "lucky" crash the program directly as you go beyond the limit).

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

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matsp View Post
    Yes, that allows 5 characters to be stored. I think tabstop was referring to your pointer variable, but I'm not sure.

    --
    Mats
    Yes indeed I was. Somehow I thought I saw code where you declared the pointer on one line and then assigned it on the next, but I don't see that now, which means I must have been hallucinating. Sorry.

  5. #20
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Thanks for the help guys. Mats I have updated my code to use arrays instead of pointers now and it seems like a good habit to use 'const char' when using pointers this way as you mentioned, thanks.
    Last edited by Subsonics; 01-07-2009 at 04:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  4. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM
  5. Replies: 4
    Last Post: 04-01-2003, 12:49 AM