Dynamic string problem !!

This is a discussion on Dynamic string problem !! within the C Programming forums, part of the General Programming Boards category; Hi, I am trying this code and it should print out ADAD DADA ------- DADA ADAD but it doesn't. Can ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    Unhappy Dynamic string problem !!

    Hi,
    I am trying this code and it should print out
    ADAD
    DADA
    -------
    DADA
    ADAD

    but it doesn't. Can someone please explain me whats wrong in this.

    void main()
    {
    char *arr[30],*tem;
    int i,j;

    tem=(char *)calloc(5,sizeof(char));
    // clrscr();

    for(i=0;i<2;i++)
    {
    arr[i]=(char *)malloc(5*sizeof(char));
    *tem=NULL;
    for(j=0;j<4;j++)
    {
    if(fmod((j+i),2)==0){ strcat(tem,"A");}
    else {strcat(tem,"D");}

    }
    arr[i]=tem;
    puts(arr[i]);
    }
    puts("-----------");
    puts(arr[1]);
    puts(arr[0]);
    getch();

    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > but it doesn't
    What did it print then?

    > void main()
    Nope - use int main
    See recent messages on the subject

    > (fmod((j+i),
    Why are you using this???
    Try regular % for integer modulo

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >arr [i] = tem;

    That's why it goes wrong.

    You want arr [0] to be ADAD and arr [1] to be DADA. First you put ADAD in tem and let arr [0] point to tem. So printing arr [0] results in ADAD. Then you put DADA in tem and let arr [1] point to tem. Printing arr [1] results in DADA. Since arr [0] also points to tem, printing arr [0] will now also result in DADA.

    Further I don't understand the 30 in the string. You could declare arr as:

    char arr [NR_OF_STRINGS][MAX_STRING_LEN];

    And use the function strcpy to copy the string from tem to the array, since that is what you actually want. You want to create an array of strings, am I correct?

    strcpy (arr [i], tem);

    Further take the notes of Salem into account and do some error checking when allocating memory. The function malloc should not be casted, read Preludes note about it.

    It isn't required, though you can if you want to. But casting malloc can hide other errors such as forgetting to include stdlib.h. So it's considered good practice to not cast malloc, calloc, and realloc.
    Last edited by Shiro; 04-21-2002 at 10:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. string problem
    By INeedSleep in forum C++ Programming
    Replies: 24
    Last Post: 11-08-2007, 10:52 PM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

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