Thread: Array print problem

  1. #1
    Registered User
    Join Date
    Feb 2008

    Question Array print problem

    Ok, so i've got a bit of a problem. I've dynamically allocated a 2d array, grabbeb input and put it into a buffer, and now would like to print the output, but am having some trouble thus far.
    Here is my code:

    ** Note: the variable size included in the malloc statement is a previously defined variable by the user ** just incase anyone was wondering why it was there

    p = (char**)malloc(size*sizeof(char*));
       if (p == NULL) merror();
       for (j=0; j<size; j++) {
    		if (p[j] == NULL) merror();
    		B: printf("Enter a name:");
    			for(k=0; k<20; k++) {
    				if (buffer[k]=='\n') {
         if (k==20) {
         printf("input too long\n");
         goto B;
       if (buffer[0]=='\0') {
         printf("Empty input. Please re-enter\n");
         goto B;
     // Here is where i need to print the output of words
     // Not too sure how to do this i.e., what for statements, and how to strcpy from the buffer to array and print it
    return 0;
    Any help is greatly appreciated

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    why not to use fgets for input?
    avoid goto
    use better indentation
    do not cast malloc - read fAQ

    to copy strings use strcpy
    to print - puts, printf or outher io-routines
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2008
    Well, I dont know how to use fgets() to count the input for a maximum of 20 characters, so i chose to use fgetc to do it character by character. I'm not sure what you mean by casting malloc, Im fairly new to C and programming in general, so I guess I will have to look that one up. As for the copying of the strings, I've been tryin to use strcpy, but I really am stuck on from what I need to copy. Im pretty sure my code is storing the names into a buffer, and I guess I need to copy from the buffer, into an array. Ive tried:
    strcpy(store, buffer[k]);
    However, I cannot get this...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. linked list from array problem
    By dcs in forum C++ Programming
    Replies: 5
    Last Post: 05-21-2004, 11:37 AM
  5. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM