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

for the love of all that is good and holy, please remove those foul GOTO statements.

the answer is easy enough, but i cant even bear to look at this code as long as there's a GOTO in there.

Instead of the GOTO, what would you recommend? I'm new to programming, so bare with me for any common beginner mistakes I may make

any sort of conditional statement is fine ... "while" is a common one to use

so instead of, say, doing this:

A:  printf("enter name : ");
    scanf("%s", gotName);

    ... stuff ...

    if (strcmp(gotName,"quit") == 0)
        goto A;

do something like this:

int isDone = 0;
char gotName[32];

while ( ! isDone)
{
    printf("enter name ");
    scanf("%s", gotName);

    if (strcmp(gotName,"quit") == 0)   
        isDone = 1;
    else 
    {
        ... stuff ...
    }
}

seriously though. im not being facetious. gotos make me ill. the use of gotos should absolutely never be done, unless there is a really, really compelling reason to do so. And I can't think of any.

they should never have been allowed to be included into the language.

other options are using "for" loops and "do/while" loops ... related useful commands include "continue" and "break" which cause the loop to be immediately run again, or immediately broken out from, respectively.

.

now here's something that might be of interest to you.

char names[3][32];   // array of 3 names, each being a string of up to 32 chars
int index =0;

...


while (index<3) {

    printf("enter name #%d : ",index);
    scanf("%s",names[index]);

    index++;
}

...

for (index=0; index<3; index++) {

    printf("name #%d : ",name[index]);

}

Hmm, thanks for the help thus far. I kno how to print a static array, but I dont know why I cannot wrap my head around this dynamic array with pointers concept. See, i just don't know where/how the input is being stored into the array, so each time i got to print i get (null)(null)etc...

Maybe the following clarifies it

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	char** p = NULL;
	const int numStrings = 2;
	char string[21];

	p = (char**)malloc(sizeof(char **) * numStrings);

	for(int nn = 0; nn < numStrings; ++ nn)
	{
		sprintf(string, "string %d\0", nn);
		p[nn] = (char*) malloc(strlen(string) + 1);
		strcpy(p[nn], string);
	}

	for(nn = 0; nn < numStrings; ++ nn)
	{
		printf("string %d = [%s]\n", nn, p[nn]);
                // free the memory as we go
		free(p[nn]);
	}
        // finally free memory pointed to by p
	free(p);
 	return 0;
}

I kno how to print a static array, but I dont know why I cannot wrap my head around this dynamic array with pointers concept.

oh, whoops. sorry. i missed where you said that this was a dynamic array.

i didnt actually read your code. i still can't see past those big ugly GOTO's

>the use of gotos should absolutely never be done
A more reasonable guideline is simply to avoid goto until you're confident that you completely understand it and its alternatives. Sadly, too many people take the "never use goto!" as some sort of silly religious mantra and don't bother to actually learn about the feature or consider it as an option.

>unless there is a really, really compelling reason to do so. And I can't think of any.
Don't take this the wrong way (or do, I don't much care), but if you can't think of any legitimate reasons to use goto, you're not qualified to tell people never to use it.

Sadly, too many people take the "never use goto!" as some sort of silly religious mantra

YOU SHALL PAY FOR YOUR INSOLENCE, BLASPHEMOUR!

but if you can't think of any legitimate reasons to use goto, you're not qualified to tell people never to use it.

actually, i have a confession. ive used GOTOs recently in the context of error handling for hardware drivers, following a convention that was established by National Instrument's source code. i swallowed my distaste because it was rather simple and concise in the way it was structured.... but i didnt want to get into that here.

to see a simple input routine run by GOTOs is just painful. Truly, theres nothing that GOTOs do that cant be done another, equally good and probably better, way. using GOTOs in professional work is usually a sign of either incompetence or laziness, so the last thing people should be teaching students is to use GOTO.

Now when you have the misfortune of inheriting some 5 year old code of 50,000 lines and hundreds of functions, and some fargin bastage has gone and put GOTOs and Globally scoped variables all over the eff-ing place --- it makes you want to hunt down Kernigan and Ritchie and ask them WTF's idea was it to allow GOTO in the first place, even against the advice of many industry professionals.


.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.