C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 17th, 2013, 01:33 AM
timodes timodes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 timodes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 29 sec
Reputation Power: 0
Search string in array and return index

Dear all, I have a problem in my code. After I enter the string I want to search, the program crash.

I have checked my code, but I still could not figure out what went wrong.

Would need your advice.

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

int findTarget(char *string, char *nameptr[], int num);
int main()
{
	int index, index2;
	int size;
	char *nameptr[100];
	char *string;

	printf("Enter the number of names: ");
	scanf("%d",&size);

	for(index=0; index<size; index++)
	{
		printf("Enter A Name: ");
		scanf("%s", &nameptr[index]);
	}

	printf("\nEnter a string to search:");
	scanf("%s", &string);

	index2 = findTarget(string, nameptr, size);

	if ( index2 == -1 )
	{
      printf("\nNo - no such name\n");
	}
	else
	{
      printf("\nYes - matched index location at %d\n", index2);
	 }
	return 0;
}

int findTarget(char *string, char *nameptr[], int num)
{
	int i=0;

	for ( i = 0 ; i < num ; i++ )
	{
	 
		if (strcmp(nameptr[i],string)==0)
		{
			return i;
		}
	}
 
	return -1;
}

Reply With Quote
  #2  
Old February 17th, 2013, 01:47 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,139 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 23 h 17 m
Reputation Power: 1974
char *string;

You're using an uninitialized pointer. It contains a garbage address that is most likely pointing to memory that does not belong to you, in which case the operating system protects itself and others by terminating your program with extreme prejudice.

Either malloc enough memory to string to accommodate the largest possible input string, or declare it as a char array.

Since each entry of nameptr will be in the same boat as string is, be sure to malloc for them too when you add strings, though in that case you could use the length of the string plus one.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Search string in array and return index

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap