INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Loading "C" Array from keyboard crashes.

Loading "C" Array from keyboard crashes.

(OP)
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"

RE: Loading "C" Array from keyboard crashes.

2
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);

RE: Loading "C" Array from keyboard crashes.

(OP)
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(){
	/*Declarations*/
	int length = 100;
	char **words;
	char temp[128];

	int i = 0;
	int size = 0;

	/*allocation*/
	words = malloc(length * sizeof(char*));

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

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

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

	/*Free Array*/
	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> 

RE: Loading "C" Array from keyboard crashes.

In your loop to print each item, the free() should be like this...

CODE

free(words[ i ]); 


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close