0
#include <stdlib.h> #include <stdio.h> #include <string.h> int main(){ int n, i, check=0; char first_name[20]; char current_name[20]; printf("Enter n, followed by n last names (each last name must be a single word):"); scanf("%d", &n); scanf("%s", &first_name[20]); for (i=1; i<n; i++){ scanf("%s", &current_name[20]); if ( strcmp(first_name[20], current_name[20])==0) check = 1; } if (check == 1) printf("First name in list is repeated."); else printf("First name in list is not repeated."); system("pause"); return 0; } 

I'm using Dev C++, the error I get is this:

23:9 [Warning] passing argument 1 of 'strcmp' makes pointer from integer without a cast [enabled by default]

The programs runs, but it crashes after I type a few names in.

1
  • It fixed then accept answer. Commented Feb 10, 2014 at 5:12

4 Answers 4

2
strcmp(first_name[20], current_name[20])==0) 

Is like not valid insead use strcmp(first_name,current_name) also for

scanf("%s", &first_name[20]); use scanf("%s",first_name) instead.

Sign up to request clarification or add additional context in comments.

Comments

0

You didn't use strcmp() properly. And when passing a char [] to a function, you just need to use its name.

So you need to fix the following issues:

  1. Change

    if ( strcmp(first_name[20], current_name[20])==0) 

    to

    if ( strcmp(first_name, current_name) ) 
  2. Change

    scanf("%s", &first_name[20]); ... scanf("%s", &current_name[20]); 

    to

    scanf("%s", first_name); ... scanf("%s", current_name); 

Comments

0

The other answers here will help if you only want to work with one string. If you want to work with and array of strings as it seems you do, by the output you're printing in the loop, then you should declare an array of strings instead of a single string.

char first_name[20]; 

declares an array of chars (which is a single string if any of those characters are NUL). You seem to want to be working with an array of strings, so you need a two dimensional array of chars (or an array of char pointers and a malloc for each string):

char first_name[20][MAX_NAME_LENGTH]; 

where MAX_NAME_LENGTH is defined above like:

#define MAX_NAME_LENGTH 64 

Then you'll be able to do stuff like:

strcmp(first_name[i], current_name[i]) 

Since first_name[i] will decay to a char *.

Comments

0

In c/c++, a string is just an array of char. To access an array element, you use pointer. And to access a string from the beginning, you have to provide the pointer to the beginnning of the string.

Strcmp and scanf takes pointer to an array of char (thus, string):

int strcmp ( const char * str1, const char * str2 ); int scanf ( const char * format, ... ); 

They need the pointer to the beginning of the string. You can write either:

scanf("%s", first_name); strcmp(first_name, current_name) == 0 

or

scanf("%s", &first_name[0]); strcmp(&first_name[0], &current_name[0]) == 0 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.