malloc

This is a discussion on malloc within the C Programming forums, part of the General Programming Boards category; char *pointer[100]; . . . int input() { int i=0; int length; char help[100]; printf("Input: "); scanf("%s",help); while((strcmp(help,"ENDE")!=0)&&(i<100)) { length=strlen(help); ...

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

    malloc

    char *pointer[100];
    .
    .
    .
    int input()
    {
    int i=0; int length; char help[100];

    printf("Input: ");
    scanf("%s",help);
    while((strcmp(help,"ENDE")!=0)&&(i<100))
    {
    length=strlen(help);
    pointer[i]=(char*)malloc((length+1)*sizeof(char));
    pointer[i]=help;
    i++;
    printf("Input: ");
    scanf("%s",help);
    }
    return i;
    }

    if I enter a word and assign it to pointer[i] (how showed above), the program overwrites all elements (pointer[0] until pointer[i-1]) with this last entered word. if I want to output all elements at the end of the program (not showed above) the program puts out strange signs (always the same "smiley").

    Whats going wrong here??????

  2. #2
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Code:
    char *pointer[100];
    ...
    pointer[i]=(char*)malloc((length+1)*sizeof(char));
    Don't do that! It does not make any sense. You should use an array of structures to save the strings.
    Code:
    struct{
        char *pointer;
    }mystruct[100];
    ...
    length=strlen(help);
    mystruct[i].pointer = malloc((length)*sizeof(char));
    ...
    for(k=0; k < length; k++)
    {
        mystruct[i].pointer[k] = help[k];
    }
    [edit]
    Hmm .. I've just checked my suggested source code and found that it doesn't work for certain string sizes (strange output). Maybe you should use char arrays of fixed size in the structure. That should fix the problems.
    Code:
    struct{
        char pointer[100];
    }mystruct[100];
    ...
    length=strlen(help);
    ...
    for(k=0; k < length; k++)
    {
        mystruct[i].pointer[k] = help[k];
    }
    [/edit]

    And better learn how to use fgets() instead of scanf(). It will save you lots of trouble. Never forget: fgets() does not remove the '\n' at the end of a line!

    You should use a for loop:
    Code:
    for(x=0;x<100;x++)
    {
        printf("Input: ");
        scanf("%s",help);
        if(strcmp(help,"ENDE")==0)
        {
            break; //leave the for loop
        }
        length=strlen(help);
        ....
    }
    Last edited by Sargnagel; 11-10-2002 at 08:19 AM.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    First, declaring an array of pointers to char is a good way. But that is not the problem here, the problem is this:

    pointer[i]=help;

    To copy strings, you should use the function strcpy (string copy). For a definition of strcpy and a quick reference to C standard functions, look here:
    http://www.acm.uiuc.edu/webmonkeys/b...14.html#strcpy

    Casting of malloc isn't also necessary in ANSI C, just include stdlib.h.

  4. #4
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Originally posted by Shiro
    First, declaring an array of pointers to char is a good way
    Yes, of course it is okay! I should have pointed out, it was the conjunction with malloc() that made me think:

    pointer[i]=(char*)malloc((length+1)*sizeof(char));

    I don't see how this would do any good. As far as I understand Sylli22 wants to store pointers to arrays (each containing an input string) in the array of pointers char *pointer[100].
    But the line of source code above won't do the job.
    Please, correct me, if I am wrong!

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    As I understood the code of Sylli22, he/she wants to fill the array pointer with 100 or less strings. The variable pointer is an array of 100 pointers to char.

    The mentioned line allocates space for (length+1) chars, this space is pointed to by a pointer to char. Since pointer[i] is the i-th pointer to char in the array, this is correct.

    Note that Sylli22 is creating a 2D array here where each row has a variable length, which is the string length of the string where each element of the array points to. So pointer[i] points to the i-th string in the 2D array.

  6. #6
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Ah! Now I got it! Thank you for your explanation, Shiro.
    I was just wondering "How the hell would you access the allocated space?" .. I was thinking 1D not 2D.
    Last edited by Sargnagel; 11-10-2002 at 08:57 AM.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    malloc

    thank you, now, it works very good. the mistake was the missing strcpy(); thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 11:26 AM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 08:08 AM

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