Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-19-2008, 09:56 AM   #1
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:

#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: ");

     for(i=0; i<20; i++) {
       if (buffer[i]=='\n') {
     if (i==20) {
     printf("Input too long!\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++) {
		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;
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

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

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

Rep: Reputation: 199Reputation: 199
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...

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

#define BUFSZ 512
static char buffer[BUFSZ];

    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) */


Last edited by bigearsbilly; 03-19-2008 at 10:52 AM.
Old 03-19-2008, 11:23 AM   #3
Senior Member
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
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
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
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

#define MAX_INPUT_LEN 20
to make that clear.


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:38 AM.

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