Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading "C" Array from keyboard crashes. 2

Status
Not open for further replies.

Poduska

Technical User
Dec 3, 2002
108
US
Newbie here, Access programmer VBA for years but trying to learn "C" is killing me!

Simple start of book exercise,
Using "GCC" compiler

What my code should do:
Read each keyboard entry into Array called "Words"
Keep accepting keyboard entries until the word "Devo" then print out each entry within the "Word" array then terminate.

When I run it I can enter the first value "hello World" and all is well, when I enter the second word "Bacon" it crashes with ugly non descriptive error messages.

I THINK it is crashing at the statement
strcpy(words[size],temp);
after I have entered "Bacon" as my second entry.

Am I declaring my Array incorrectly? The concept is a bit foreign as I tend to think in "Tables", "Records" and "Fields"

Why is it not accepting additional entries?

Thank you!

Code:
int main(){
 char *words[100];
 char *temp;

 int i = 0;
 int size = 0;

 do{
  gets(temp);

  strcpy(words[size],temp);

  size++;
 
 }while(strcmp(words[size - 1], "devo") != 0 && size < 100);
 
 for(i = 0; i < size; i++){
  printf("%s\n", words[i]);
 }
 return 0;
 }


Output
Code:
[3:31:23 PM] poduskas: C:\Users\Poduska>a
hello world
bacon
Crashes after the second word entered via keyboard "Bacon
 
When you declare a pointer such as char *words[100], the pointers have to point somewhere otherwise you are copying to a random location in memory
Code:
int tempsize;
  gets(temp);
  /* At this point, you have a string and you can find out how long it is */
  tempsize = strlen(temp) + 1;
  /* now allocate somewhere to put it */
  words[size] = malloc(tempsize);

  /* Now we can copy because there is somewhere to copy the data to */
  strcpy(words[size],temp);
 
Thanks xwb for pointing the way. A Star of course

I wanted to post what I was finally able to get to work to start my project in case it can help others.

Pretty basic but one must start somewhere.

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

int main(){
	[COLOR=#4E9A06]/*Declarations*/[/color]
	int length = 100;
	char **words;
	char temp[128];

	int i = 0;
	int size = 0;

	[COLOR=#4E9A06]/*allocation*/[/color]
	words = malloc(length * sizeof(char*));

	[COLOR=#4E9A06]/*Loop to populate Array*/[/color]
	do{
		gets(temp);
		words[size] = malloc(strlen(temp) + 1);
		strcpy(words[size],temp);
		size++;

 	[COLOR=#4E9A06]/*Test for termination phrase*/[/color]
	}while(strcmp(words[size - 1], "zzzzz") != 0 && size < length);
 	[COLOR=#4E9A06]/*End of populate array loop*/[/color]

	[COLOR=#4E9A06]/*Loop through Array and print each item*/[/color]
	for(i = 0; i < size; i++){
		printf("%s\n", words[i]);
	        [COLOR=#4E9A06]/*Free Memory*/[/color][COLOR=#3465A4][/color]
		free(words[size]);
	}

	[COLOR=#4E9A06]/*Free Array*/[/color]
	free(words);
	return 0;
 }


Here is the output.
I typed in each name then pressed enter, and the last "zzzzz" was my flag to stop.
The code then loops through the array and prints each array entry.

Code:
C:\Users\Family>gcc GatheringLinesFromConsole.c -ansi -pedantic

C:\Users\Family>a.exe
dog
cat
rat
Lemur
zzzzz
dog
cat
rat
Lemur
zzzzz

C:\Users\Family>
 
In your loop to print each item, the [tt]free()[/tt] should be like this...

Code:
            free(words[ i ]);


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top