LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-19-2008, 09:56 AM   #1
bnixon10
LQ Newbie
 
Registered: Oct 2005
Posts: 14

Rep: Reputation: 0
Post C Array Problem


Hi everyone. I have a quick question regarding a 2d dynamic array, and how to print it.
Here is the code I have so far:

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

#define merror()   {printf("Memory allocation problem \n");exit(1);}

int main()
{
int i,j,k,z;
int size;
char buffer[21];
char buf[21];
char** p;

/* For entering the amount of names */
A: printf("How many names do you want to enter: ");
   fflush(stdout);

     for(i=0; i<20; i++) {
       buffer[i]=fgetc(stdin);
       if (buffer[i]=='\n') {
         buffer[i]='\0';
         break;
       }
     }
     if (i==20) {
     printf("Input too long!\n");
     while(fgetc(stdin)!='\n');
     goto A;
   }
   if (buffer[0]=='\0') {
     printf("empty input\n");
     goto A;
   }

   size = atoi(buffer);
   printf("You will enter %d names\n", size);
   
   p = (char**)malloc(size*sizeof(char*));
   if (p == NULL) merror();
   for (j=0; j<size; j++) {
		p[j]=(char*)malloc(21);
		if (p[j] == NULL) merror();
		B: printf("Enter a name:");
		fflush(stdout);
			for(k=0; k<20; k++) {
			buffer[k]=fgetc(stdin);
				if (buffer[k]=='\n') {
				buffer[k]='\0';
			break;
       }
     }
     if (k==20) {
     printf("input too long\n");
     while(fgetc(stdin)!='\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;
 }
Here is a quick example of what the program is supposed to do:
User inputs how many words he/she wants
It dynamically allocates it, and for the words they can not be longer than 20 chars (i.e. my malloc statement of 21)
Then, my problem, is taking those entered words, and printing them

Ex..
Enter how many words: 3
You will enter 3 words
Enter word: Bob
Enter word: Joe
Enter word: Bill
You entered
Bob
Joe
Bll

Any help is greatly appreciated
 
Old 03-19-2008, 10:51 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,282

Rep: Reputation: 172Reputation: 172
blimey a bit of a mess :-)
why allocate 21 bytes when you have 1Gb of RAM?
or is it an embedded wristwatch?


a simplified example...

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


#define BUFSZ 512
static char buffer[BUFSZ];

main()
{
    int count;
    char ** p;
    int t;

    puts("how many things?");
    fgets(buffer, BUFSZ, stdin);
    count = atoi(buffer);
    p = (char **) malloc(count * sizeof (char *));

    for (t=0; t < count; t++) {
        printf("enter no. %d\n", t+1);
        fgets(buffer, BUFSZ, stdin);
        *(strchr(buffer , '\n')) = '\0';
        p[t] = (char *) malloc(sizeof buffer);
        strncpy(p[t], buffer, sizeof buffer);
    }

    for (t=0; t < count; t++) {
        printf("got:%s\n", p[t]);
    }

    /* don't need to free for this example
     * though it would be needed in
     * a long running program
     * you need to free each allocation
     */
    for (t=0; t < count; t++) {
        free(*(p+t)); /* alternative syntax to p[t] (what really happens) */
    }
    free(p);

}

Last edited by bigearsbilly; 03-19-2008 at 10:52 AM.
 
Old 03-19-2008, 11:23 AM   #3
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
I'd follow Billy's advise about freeing memory or your professor may give you an F.
 
Old 03-19-2008, 11:33 AM   #4
bnixon10
LQ Newbie
 
Registered: Oct 2005
Posts: 14

Original Poster
Rep: Reputation: 0
The reason for the 21 char buffer is for the fact that, when the user input a word or a number, the number cannot be longer than 20 characters, which is also the reason I used fgetc() to get it character by character, because I didn't know how to count the input and check for length using fgets()

Last edited by bnixon10; 03-19-2008 at 12:13 PM.
 
Old 03-19-2008, 09:28 PM   #5
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by bnixon10 View Post
The reason for the 21 char buffer is for the fact that, when the user input a word or a number, the number cannot be longer than 20 characters,
Reasonable, but you should have said

Code:
#define MAX_INPUT_LEN 20
to make that clear.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
array.length in C problem manolakis Programming 2 01-31-2007 07:47 PM
A problem in declaring an array in C... pittopitto Programming 10 08-03-2006 02:28 AM
array problem pranavojha Programming 4 03-21-2006 01:00 AM
Array problem collern2 Programming 1 03-05-2006 09:19 AM
LaTeX Problem: \array Config Programming 1 06-30-2002 05:00 AM


All times are GMT -5. The time now is 06:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration