Thread: Trying to understand arrays and pointers better need some input.

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    18

    Trying to understand arrays and pointers better need some input.

    When I compile why am I getting an unknown file size error for the last function. The array isn't defined so why is size an issue?

    Code:
    include "stdafx.h"
    
    
    void text_test(char text[]);
    void text_test2( char *ptrtext);
    void text_test3( char *array);
     int main()
    
    {
        text_test("hello");
        text_test2("world");
        text_test3("again");
        
    return 0;
    
    }
    
    void text_test(char text[])
    {
        //char text[128];
        //ptrtext=text;
    
         printf("%s\n",text);
    }
    
    void text_test2( char *ptrtext)
    {
    
        //char 2text[];
        //*ptrtext=2text;
        
        printf("%s\n",ptrtext);
    }
    
    void text_test3( char *array)
    {
        char char_array[];
          *array = char_array[];
    
            printf("%s\n",char_array);
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    "isn't defined"?! It's not just defined, it's declared right there on line 37, but in such a way that it will generate a compile error because the size is not specified and initialisation data is not provided.

    WHat do you want it to do?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    18
    Quote Originally Posted by iMalc View Post
    "isn't defined"?! It's not just defined, it's declared right there on line 37, but in such a way that it will generate a compile error because the size is not specified and initialization data is not provided.

    WHat do you want it to do?

    I was just trying to get again to print using by setting pointer equal to an array. What kind of initialization is need to make it work?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I was just trying to get again to print
    Code:
    void text_test3( char *array)
    {
          char char_array[5];    // create an array
          *array = char_array[]; // the wrong way around
          char_array = array;    // not allowed to assign to an array
          printf("%s\n",char_array);
    }
    So , No that can't be done using assignment you need to call strcpy()

    Code:
    void text_test3( char *array)
    {
          char char_array[5];
          strcpy( char_array, array );
          printf("%s\n",char_array);
    }

    Kurt
    Last edited by ZuK; 06-09-2013 at 12:38 AM.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    void text_test3( char *array)
    {
          char char_array[5];
          strcpy( char_array, array );
          printf("%s\n",char_array);
    }
    char_array[5] will be too small if the OP wants to call text_test3() with the string literal "again".

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    It's safer to use strncpy().

    Code:
    void text_test3(char *array)
    {
        char char_array[40];
        strncpy(char_array, array, sizeof(char_array));
        char_array[sizeof(char_array) - 1] = 0;     /* Because strncpy() doesn't terminate on overflow */
        printf("%s\n", char_array);
    }

  7. #7
    Registered User
    Join Date
    May 2013
    Posts
    18
    Quote Originally Posted by KenJackson View Post
    It's safer to use strncpy().

    Code:
    void text_test3(char *array)
    {
        char char_array[40];
        strncpy(char_array, array, sizeof(char_array));
        char_array[sizeof(char_array) - 1] = 0;     /* Because strncpy() doesn't terminate on overflow */
        printf("%s\n", char_array);
    }
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-22-2011, 04:18 PM
  2. Understand pointers
    By Aphex in forum C Programming
    Replies: 3
    Last Post: 08-12-2010, 09:07 PM
  3. i cant understand something in pointers
    By nik2 in forum C Programming
    Replies: 2
    Last Post: 02-12-2010, 01:26 PM
  4. Input on arrays and pointers
    By cjohnman in forum C Programming
    Replies: 2
    Last Post: 05-01-2008, 01:57 PM
  5. Arrays of pointers (input)
    By tyskater in forum C Programming
    Replies: 10
    Last Post: 12-17-2003, 12:00 AM