Thread: initialize function

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    19

    initialize function

    is this how i would make a function to clear arrays? and set every element in char arrays to \0?
    Code:
    void initialize(intarray[], chararray[])
    {
     if(intarray[index] != 0)
     {
       intarray[index] = 0;
       index++;
     }
    index = 0
     if(chararray[index] != \0)
     {
       chararray[index] = \0;
       index++;
     }
    }
    thanks
    Last edited by nospammax; 04-24-2011 at 09:31 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Your for loops are ill formed...

    It's supposed to be for(InitialValue; ExitCondition; Counter)

    For example...
    Code:
    int x;
    
    for (x = 0; x < 10; x++)
      DoSomethingImpressive();
    Also, you need to tell the function how big the arrays are...
    Code:
    void Intialiaze(int *Array, int ArraySize)
      {  // do it here
       }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. You need to know the number of elements in the array, then count from zero to size-1.


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

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Code:
    Hi,
    
    Ya..By following CommonTater's comment, it will work.One correction in your code.
     For representing NULL you should use '\0' instead of simply \0
    
    Rajisankar

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    just use bzero() function defined string.h

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Dibyayan Chakra View Post
    just use bzero() function defined string.h
    ..... if you want a compiler-specific, non-standard, solution.

    An option that is actually specified in the C standard is memset().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    You didn't mention if you want to initialize the arrays several times during program execution, or whether you want to have the arrays initialized one time at the beginning of program execution.

    If you simply want to start your program with initialized arrays, you can do so when you define the array, e.g.
    Code:
    int test[100] = { 0 };
    This will initialize all 100 elements of array test with a 0 value.

    Kevin

  8. #8
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    HI Grumpy. I think that bzero() is part of the C99 standard library. It just isn't available in ANSI C. Can anyone confirm?

    Kevin

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Here's a site that says bzero is "partially obsolete"
    Copying and Concatenation - The GNU C Library

    2nd site
    http://pubs.opengroup.org/onlinepubs...ons/bzero.html

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kmess
    I think that bzero() is part of the C99 standard library. It just isn't available in ANSI C. Can anyone confirm?
    I can confirm that bzero is not part of C99's standard library.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kmess View Post
    HI Grumpy. I think that bzero() is part of the C99 standard library. It just isn't available in ANSI C. Can anyone confirm?

    Kevin
    Nope not C99 and not present on either of my library/header sets.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Dibyayan Chakra View Post
    just use bzero() function defined string.h
    Which compiler are you using?

    bzero() appears to be obsolete.

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by kmess View Post
    HI Grumpy. I think that bzero() is part of the C99 standard library. It just isn't available in ANSI C.
    C99 is ANSI. That is to say, ANSI adopted ISO/IEC 9899:1999 (AKA C99), just like they adopted ISO/IEC 9899:1990 (AKA C90, or, usually used to mean the same thing, C89).

    bzero() was not in C89. It was a BSD function adopted by POSIX, and has been removed as of POSIX 2008.

  14. #14
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    That's probably why I'm finding it in my library - I use BSD. Cool to know.

    Kevin

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by cas View Post
    C99 is ANSI. That is to say, ANSI adopted ISO/IEC 9899:1999 (AKA C99), just like they adopted ISO/IEC 9899:1990 (AKA C90, or, usually used to mean the same thing, C89).
    It was actually the other way around.

    ANSI developed and ratified the 1989 C standard, and it was later adopted (with minor changes of labeling) by ISO. That is why the 1989 C standard is sometimes referred to as C90, particularly by Europeans: it was ratified by ANSI in 1989, and later adopted by ISO in 1990.

    I'm not exactly sure exactly which organisation did what with the 1999 C standard (the whole process was somewhat quieter) but would be surprised if there was a different sequence.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initialize struct in function
    By lordkrandel in forum C Programming
    Replies: 19
    Last Post: 04-17-2011, 12:37 AM
  2. Initialize elements to zero
    By Eman in forum C++ Programming
    Replies: 5
    Last Post: 03-16-2011, 03:59 PM
  3. Replies: 8
    Last Post: 02-14-2010, 04:14 PM
  4. Initialize COM
    By The15th in forum Windows Programming
    Replies: 7
    Last Post: 01-20-2002, 09:55 PM
  5. Initialize an array
    By cheesehead in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2001, 04:28 PM