Having problem printing **string

This is a discussion on Having problem printing **string within the C Programming forums, part of the General Programming Boards category; Hi I am new to C (been working with it for about 2 months by myself) and I have a ...

  1. #1
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68

    Having problem printing **string

    Hi I am new to C (been working with it for about 2 months by myself)
    and I have a problem I found an exercise where i am to create a twodimensional array of strings and rearrange after taking in consideration some criteria.... Anyway I am in the beginning and I have allocated space for my array and read the strings but for some reason it prints only the first 3 letters of each string....
    Code:
    #include <stdio.h>
     #define STR_LEN 25  
      void creation(char **,int);
     
    main()
    {
     char **string;
     int i,size;
     
     	printf("How many rows do you want?  ");
    	scanf("%d",&size);
    	fflush(stdin);
    	string=(char **) malloc(size*sizeof(char *));
    	creation(string,size);
    	for (i=0;i<size;i++)
    		fputs(*(string+i),stdout); //i've also tried printf("%s",*(string+i));
    	system("pause");
    	
    }
    
    void creation(char **str,int size)
    {
     int i;
     	
     	for(i=0;i<size;i++)	{
     		*(str+i)=(char *) malloc(STR_LEN*sizeof(char));
     		printf("String No%d: ",i+1);
     		fgets(*(str+i),sizeof(*(str+i)),stdin);
     		fflush(stdin);
    	}
    }
    If someone can help me I d really appreciate it...Oh and if someone can explain me what is better for printing (printf,fputs....) and reading (sscanf,scanf,fgets,....)

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The mistake is here
    Code:
     		fgets(*(str+i),sizeof(*(str+i)),stdin);
    *(str+i) is a pointer so sizeof(*(str+i)) returns 4 on a ia32. -> fgets will stop reading after 4 characters.
    BTW char **string is not a 2-dim array of strings.
    It's eiter an array of strings or a 2-dim array of char.

    Kurt

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > fflush(stdin);
    Read the FAQ - stdin isn't a stream you can fflush

    > string=(char **) malloc(size*sizeof(char *));
    Read the FAQ on casting malloc. You've just hidden the fact that you didn't include stdlib.h

    > fgets(*(str+i),sizeof(*(str+i)),stdin);
    Rather than all this str+i, just write
    fgets(str[i],STR_LEN,stdin);

  4. #4
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    ZuK and Salem thanks a lot. Yeah you are right ZuK that's waht i meant (2-dim array of char). I see what you mean about size of, I had originally used STR_LEN but i changed it (dont know why though, shows my inexperience).... Anyway thanks a lot.
    By the way does it matter if i use *(str+i) instead of str[i], I wondered about that

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Phoenix_Rebirth
    By the way does it matter if i use *(str+i) instead of str[i], I wondered about that
    There is no difference. In my opinion str[i] is easier to read.
    Kurt

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I see what you mean about size of, I had originally used STR_LEN but i changed it (dont know why though, shows my inexperience)....
    It doesn't matter which one you use. I prefer sizeof().

    And maybe you should free() everything, too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It should be noted that the sizeof operator does not:
    a - Give the length of an array passed as an argument.
    b - Give the length of any "array" allocated dynamically.

    It will only give you the "length" of a "string", if it is in fact an array which has been declared in the scope of your using the sizeof operator.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Problem Printing to File
    By jamez05 in forum C++ Programming
    Replies: 10
    Last Post: 09-26-2006, 11:20 AM
  3. Replies: 1
    Last Post: 06-07-2006, 09:42 AM
  4. Message printing problem
    By robert_sun in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 05:05 AM
  5. problem with printing a structed array using for loop
    By Prezo in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2002, 09:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21