2

I need to allocate arrays of chars by malloc() and then print them.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main (void){
    int i, n, l;
    char **p;
    char bufor[100];
    printf("Number of strings: ");
    scanf("%d", &n);
    p=(char**)malloc(n*sizeof(char*));
    getchar();
    for (i=0; i<n; ++i){
        printf("Enter %d. string: ", i+1);
        fgets(bufor, 100, stdin);
        l=strlen(bufor)+1;
        *p=(char*)malloc(l*sizeof(char));
        strcpy(*p, bufor);
    }
    for (i=0; i<n; ++i){
        printf("%d. string is: %s", i+1, *(p+i));
    }

    return 0;
}

I have a problem with printing those strings. I don't know how to get them.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Defozo
  • 2,946
  • 6
  • 32
  • 51
  • 6
    [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Dec 19 '15 at 17:16
  • 3
    So you allocate an array of pointers to characters, then allocate memory and assign that always to the _first_ pointer to characters, then wonder why the rest isn't filled? Use `*(p+i)= malloc(...` – Paul Ogilvie Dec 19 '15 at 17:18

2 Answers2

2

The problem, as I see, is you're overwriting the same location over and over again. This way

  1. You're losing the previously allocated memory.
  2. Only the last entry persists.

You need to change your code like something

    p[i]=malloc(l);
    strcpy(p[i], bufor);

to use the next pointer-to-pointer inside the loop.

That said,

  • Always check the return value of malloc() and family for success before using the returned pointer.
  • No need to cast the return value of malloc() and family in C.
  • sizeof(char) is defined as 1 in C. No need of multiplying a size with that.
  • Instead of using malloc() and strcpy(), you can also consider using strdup() to achieve the same result.
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Thank you for the answer. It's working now. BTW. I read somewhere on stackoverflow that sizeof(char) is a matter of style and generally should be written. – Defozo Dec 19 '15 at 17:27
  • imo, using sizeof(char) shouldn't be replaced with a 1 (or nothing), because including it makes the operation explicit, therefore making it easier for others to understand your code. it is needed especially in places where you do important and potentially dangerous operations like memory allocation. – Noobay Aug 08 '17 at 19:30
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
   char  *names[6] ;
   char n[50] ;
   int  len, i,l=0 ;
   char *p ;
   for ( i = 0 ; i <= 5 ; i++ )
    {
         printf ( "\nEnter name " ) ;
         scanf ( "%s", n ) ;
         len = strlen ( n ) ;
         p = malloc ( len + 1 ) ;
         strcpy ( p, n ) ;
         names[i] = p ;
         if (l<len)
         l=len;
    }


     for ( i = 0 ; i <= 5 ; i++ )
    printf ( "\n%s", names[i] ) ;
    printf("\n MAXIMUM LENGTH\n%d",l);
    return 0;
}