Thread: 3 Questions

  1. #1
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472

    3 Questions

    1. Why do lots of programmers use BUFSIZ as a size for their large arrays?? how big is it anyway??



    2. Check this FAQ entry Option 3 , the example where the author uses a char array BUFSIZ big and a strtol() function.

    when the user inputs a number in that char array , how will numbers fit in it??? like if he enters the number '42' , how will it be stored in that CHAR array???



    3. i've saw someone doing this before :
    Code:
    #include<stdio.h>
    
    int main()
    {
    char *p;
    
    p = "hello world";
    
    puts(p);
    
    return 0;
    }
    im not used to point to a string without allocating space for it first and pointing to that space , how does the previous example work???



    appreciate your help..
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    1) BUFSIZ is a standard macro that's the size of the buffer used by setbuf. It's generally a good large size for an input buffer. But we don't know the size, that's not specified.

    2) [edit]Wow, I really confused myself.[/edit]
    When reading from the stream, everything is a character. When the user types 42, it's actually the characters '4' and '2', not the number 42. That conversion is only made after the characters are extracted from the stream. So 42 fits in a char array simply because no conversions were performed.

    3) The implementation handles memory for string literals, all you have to do is use them or save their address with a pointer provided you agree not to try and modify their contents.
    Last edited by Prelude; 06-20-2004 at 06:31 PM.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    12
    1) BUFSIZ is set to 256 in <stdio.h> http://www-ccs.ucsd.edu/c/stdio.html#BUFSIZ
    2) the character will be stored in arrayname[number] number is in your case 42
    3) *p="hello world"; is the same as saying p[ ] = "hello world"; it allocates space to the size of the string specifically plus one extra for the '\0' . You could later use p as p[1],p[2],p[3], etc ...

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    1. No BUFSIZ is not set to 256 in stdio. It changes for each system. for me it is 8192.
    2. It will not be put in array[numeryouenter] if I enter 82 it will be like this
    Code:
    buffer[0]=8;
    buffer[1]=2;
    It won't be in buffer[81];
    3. You can access it with p[1], but you cannot edit the values or else you will get a segfault. This is just a pointer and that is read only memory(ROM)
    Last edited by linuxdude; 06-20-2004 at 06:31 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >1) BUFSIZ is set to 256 in <stdio.h>
    That's the lower limit. It's allowed to be more.

    >2) the character will be stored in arrayname[number] number is in your case 42
    What?

    >3) *p="hello world"; is the same as saying p[ ] = "hello world";
    No, no it's not. The former points a pointer to a string literal, the latter initializes an array with "hello world" and sets the size accordingly.
    My best code is written with the delete key.

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    gotcha by a second prelude

  7. #7
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Thanks a bunch Prelude , linuxdude and qodsec
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM