473,408 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

When i enter strings during execution i get error

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include<stdio.h>
  4. #include<conio.h>
  5. #include<string.h>
  6. main()
  7. {  char (*arr)[5];
  8.      int i,j,l;
  9.      int flag=0;
  10.  // char *arr[5]={"sahil","abhi","sahil","amit","john"};
  11.   for(i=0;i<5;i++)
  12.   gets(arr[i]);  //the problem is in this code
  13.  
  14.  
  15.   for(i=0;i<5;i++)
  16.   {
  17.     for(j=i+1;j<5;j++)
  18.     {     l=strcmp(arr[i],arr[j]);
  19.     if(l==0)
  20.     { printf("duplicate entry");
  21.     flag=1;
  22.     break;
  23.     }
  24.   }
  25.   }
  26.   if(flag==0)
  27.   printf("unique elements");
  28.   }
  29.  
  30.  
  31.  
  32.  
Jul 28 '10 #1
4 1164
code green
1,726 Expert 1GB
what's d errr
Jul 28 '10 #2
newb16
687 512MB
Prblm s unntlzd arr. It pnts nwhr.
Jul 28 '10 #3
Joseph Martell
198 Expert 128KB
arr is a pointer to an array of 5 char elements, not a 5-element array of char pointers.

I think that what you want to do is
Expand|Select|Wrap|Line Numbers
  1. char arr[5][6];
Right now you are passing in a buffer of 1 char, not a 5-char buffer. The definition for arr that has been commented out accomplishes similar goals.
Expand|Select|Wrap|Line Numbers
  1. char *arr[5]={"sahil","abhi","sahil","amit","john"};
This definition creates arr as a 5-element array of char pointers where char[0] points to a char array containing "sahil", etc.

I could be wrong though. I haven't limited C/C++ experience.
Jul 28 '10 #4
Oralloy
988 Expert 512MB
You never initialized the entries of arr[] to appropriate memory buffers.

jbm1313 gives a basic solution which will let you accept strings of up to length five.

Unfortunately its a bad way to implement, as it allocates fixed buffers, but gives you no guarantee of memory safety. If the user enters a string that is too long, it'll propagate down the array with all sorts of interesting results.

What you really should do is use a length guarded read into an input buffer and then copy from the input buffer to an allocated data buffer and insert that into your data array. Throw an error, if the user's input overruns the input buffer.

Still, this looks like a homework assignment, so I'll leave the implementation details to you. I hope that our comments above are enough to help you get it done.
Jul 28 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Xelah | last post by:
I was trying to make little counter on my website. Im generatin png image and everyting works just fine on my computer. But when I send all scripts to the server I can only see proper image under...
10
by: chandra.somesh | last post by:
Hi I recently had to write a small code in a competition ,but my code was rejected cause it failed in 1 of test cases. The problm was .....we are given vector of strings....each string...
14
by:  | last post by:
having a spot of trouble writing this one. if you are so inclined and have a moment, i'd really appreciate your insight. i have what amounts to a purchase order type of setup...a descriptive...
3
by: Saf | last post by:
I have an asp registration form, generating a html email, using cdosys. When a hotmail user registers they receive an empty email or so it seems, until you forward the email then the contents...
3
by: comasoud | last post by:
Hi all, I'm having a problm woth an application developed in Access 2003. I have developed an application on Ms Access 2003. the folder where i stored it was c:\masoud\devlopment\ However when...
18
by: luckyyyyyy | last post by:
I appriate your work but... guys i have another problm.....i wana skip first 9 lines from my data file and after skipping i wana read x,y,z at the end of file. i did but after while loop is end it...
1
by: ssknov | last post by:
hi i had created as SP to delete the record using where condtn.if the record doesnt exists the SP will throw an error as "No such record found" and return 0 . But my c# code gets -1 rows...
1
by: siri11 | last post by:
Hi!!!!!!!!!! How many lines of code can we write in c#.net (Windows Application) for a single form.In my application for one form i have written about 3850 lines.Is there any problem of...
3
by: Microsoft Newsserver | last post by:
Hi, We have situations occasionally where the SQL server is extremely slow to respond. The problm is that the user is left with loading page and has no idea what in heaven is going on. Does...
1
Fary4u
by: Fary4u | last post by:
Hi i'm trying to design the picture gallery ever thing works fine but whn i've make test movie or export the swf file it's runs fine but problme i'm using this with in movie clip & i think...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.