HELP Please Please (malloc on an array) 
Author Message
 HELP Please Please (malloc on an array)

All I need to do is create an array of strings in memory. Not a hard task at
all. I can do this easily using the following code but I have had problems
trying to do this dynamically with malloc etc. I haven't used malloc much
and don't have the time to get it by trial and error.

Could someone please help me as I need to get this done very quickly.

Code using standard char array
------------------------------
main()
{
char array1[1000][11];
int temp;

for(temp=0;temp<999;temp++)
   sprintf(array1[temp],"Test %d",temp);

printf("\n\nEnter a number :");
scanf("%d",&temp);
printf("\n%s",array1[temp]);

Quote:
}

Code using malloc etc
---------------------

(fill in here, Please)

Thanx in advance.

Matt Thompson
Software Technician
Christchurch Polytechnic
New Zealand



Sun, 25 Jun 1995 10:43:23 GMT  
 HELP Please Please (malloc on an array)

Quote:

>Code using standard char array
>------------------------------
>main()
>{
>char array1[1000][11];
>int temp;
>for(temp=0;temp<999;temp++)
>   sprintf(array1[temp],"Test %d",temp);
>printf("\n\nEnter a number :");
>scanf("%d",&temp);
>printf("\n%s",array1[temp]);
>}
>Code using malloc etc
>---------------------

main()
{
    char    *array1[1000];
    int      i;

    for (i = 0; i < 999; i++) {
        array1[i] = malloc(11);
        sprintf(array1[i], "Test %d", i);
    }

    printf("\n\nEnter a number: ");
    scanf(" %d", &i);
    printf("\n%s\n", array1[i]);

Quote:
}

--

Just who ARE you calling a FROOFROO Head?    |                             |




Mon, 26 Jun 1995 05:09:41 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

2. NEED HELP WITH PRITING AN ARRAY, PLEASE PLEASE HELP

3. Please help!!!!Please help!!!!Please help!!!!

4. help: Guru needed please please please

5. HELP please on QUEUES!! please, please, please, please,please...

6. PLEASE PLEASE PLEASE PLEASE

7. PLEASE PLEASE HELP HELP...question on interleaving C functions

8. Problems with malloc(), PLEASE HELP!!!

9. Problems with malloc, PLEASE HELP!!!

10. please help on malloc/free!

11. malloc problem --- help please

12. Please help with malloc failure detection

 

 
Powered by phpBB® Forum Software