Thread: fprint an array of string pointers?

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    Question fprint an array of string pointers?

    hi,

    i'm working on a project that has dynamically allocated strings placed into a dynamically allocated array. i'm trying to use fprint to print out all of the strings to standard output, but it's not working too well.

    here's my statement:
    Code:
    fwrite ((char**)rec_arr, sizeof(char*), rec_index, stdout);
    rec_arr is a dynamically allocated char** array and rec_index is the number of pointers in rec_arr. using a for-loop seems to work, but i'm not sure if i'm fully utilizing the potential of fwrite. any help would be appreciated!

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by syaorankun View Post
    hi,

    i'm working on a project that has dynamically allocated strings placed into a dynamically allocated array. i'm trying to use fprint to print out all of the strings to standard output, but it's not working too well.

    here's my statement:
    Code:
    fwrite ((char**)rec_arr, sizeof(char*), rec_index, stdout);
    rec_arr is a dynamically allocated char** array and rec_index is the number of pointers in rec_arr. using a for-loop seems to work, but i'm not sure if i'm fully utilizing the potential of fwrite. any help would be appreciated!
    Well actually your statement will write the content of rec_arr to the standard output. However rec_arr contains pointers, and not true strings. The true strings are pointed by the elements of rec_arr, so what you print is those pointers. You can not use fwrite in such a way that it would dereference those pointers and print the string to the standard output or file. That is something you must do yourself, as you noted that for works correct. You could use puts() or fputs() if you think fprintf() would be too much.
    Code:
      int i;
      for(i=0; i < rec_index; i++)
         puts(rec_arr[i]);
    I think thats the most efficient way to do this using C. Using fwrite() would be probably better if you knew the string lengths before hand. But still i think the performance hit is small.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    2
    hello!

    i'm glad to know that i can't use fprint() without using a for-loop. fortunately for my program, i do know the length of each string, so i did end up using fprint():

    Code:
    for(i = 0; i < rec_index; i++)
    	{
    		fwrite ((char**)rec_arr[i], sizeof(char*), 1, stdout);
    	}
    thanks!

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by syaorankun View Post
    hello!

    i'm glad to know that i can't use fprint() without using a for-loop. fortunately for my program, i do know the length of each string, so i did end up using fprint():

    Code:
    for(i = 0; i < rec_index; i++)
    	{
    		fwrite ((char**)rec_arr[i], sizeof(char*), 1, stdout);
    	}
    thanks!
    A small note: fwrite doesn't care about the pointer type it gets. You need not cast it. And again, why cast a 'char *', which is the type of rec_arr[i] to a 'char **'? If you have the lengths stored, you should use them on the call to fwrite. Like:
    Code:
    for(i = 0; i < rec_index; i++)
        {
            fwrite(rec_arr[i], rec_length[i], 1, stdout);
        }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's better to just use puts when you're working with strings (unless they aren't NULL terminated).
    And I suggest you avoid casting, as well. It can mask problems that you don't want.
    Like sending a char** to puts will give you a warning, because that's not what you should use as argument.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM