Array initialization to NULL gives Link error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tvnaidu
    Contributor
    • Oct 2009
    • 365

    Array initialization to NULL gives Link error

    Trying to initialize array to NULL, gives Link error (Undefined : "__clear")

    Code:
    char ip_string[16] = {0};
    may be compiler issue?.
    anything wrong?. thanks.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Your line of code is not "trying to initialize array to NULL". NULL has a pointer type; you have an array of chars.

    The linker error refers to undefined symbol "__clear". Typically, symbols that start with underscore are part of the compiler-provided compile-time or run-time environments.
    • Perhaps your compiler is improperly installed.
    • Perhaps you are missing some environment variable that permits the linker to find the run-time library.
    • Perhaps your PATH is set wrong.
    • Perhaps you need to change some compiler command line argument.

    As it happens, you can work around this problem. The default initial value for non-automatic variables that don't have initializers is zero. Your "={0}" initializer is redundant. What happens if you delete it?

    By the way, do you intend this to be an array of numbers or an array of text characters? If numbers, then it should be an array of signed char or unsigned char. If text, then your initializer should be a character constant ({'\0'}). This is a pedantic point, it doesn't have anything to do with your linker error.

    Comment

    • tvnaidu
      Contributor
      • Oct 2009
      • 365

      #3
      This array to basically I need to copy some text (numbers - like IP address notation) and send the text on serial port to display on LCD, basically I need to initialize the array to NULL, before copy the string, can I do memset to initialize to NULL.

      memset(ip_strin g, 0, sizeof(ip_strin g);
      strcpy(ip_strin g, "192.........." );

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        If ip_string is a null-terminated string, then you only need to initialize the first character of the string to null. Nothing is gained by filling the entire array with nulls.
        Code:
        char ip_string[16] = "";
        On the other hand, you might like to do something like this:
        Code:
        char ip_string[] = "XXX.XXX.XXX.XXX";
        ...
        ip_string[0] = '\0';        // Set ip_string to "".
        Notice that the array size is implied by the length of the initializer string. The advantage here is that it is much easier to see at a glance that the array size is large enough to hold an IP address string.


        By the way ... There is no easy way to say this, you are confusing NULL with null. (Sounds like Who's on First.)

        NULL [all uppercase] is a macro that is defined by several of the standard header files (stdio.h, stdlib.h, etc). The value of NULL is guaranteed to be 0. This macro is used wherever you want to indicate a pointer that doesn't point to anything (a NULL pointer).

        Null [mixed-case or all lowercase] is the name of the character used to terminate strings. The value of null is guaranteed to be '\0'. The compiler does not predefine a macro or symbol named "null". The term 'null' is similar to terms like 'function' or 'operand' in that none of these terms have any special meaning to the compiler, but programmers use them to describe what they are doing.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          char ip_string[16] = {0};
          Your initialization code works just fine.

          There must be a problem with your compiler.

          The 0 is an int which converts to a char with no problem.

          Comment

          • tvnaidu
            Contributor
            • Oct 2009
            • 365

            #6
            Thanks. It is compiler issue. I just declared and inside I am doing memset that array to zero.

            Comment

            Working...