-8

I am attempting to display a series of questions to a user, then scan in the responses. My code builds without error, but when I run it I get errors: Expecting pointer to char but found pointer to aggregate. What are the mistakes here?

#include <stdio.h>

int main ()

{

 char name[50] , lastname[50] , add[100], post[50], town[60], state[60], tel[50];

 printf ("**** PLEASE ENTER THIS CONFIDENTIAL INFORMATION****");

  printf ("\n=================================================\n=================================================");

printf ("\n\nFirst name:");
scanf ("%s",&name);

printf ("\nLast name:");
scanf  ("%s",&lastname);

printf ("\nAddress Please:");
scanf ("%s",&add);

printf ("\nPostcode:");
scanf("%s",&post);

printf ("\ntown:");
scanf ("%s",&town);

printf("\nTelephone number:");
scanf("%s",&tel);


printf ("\n\n****CONFIDENTIAL INFORMATION****");

printf ("\n=================================================\n=================================================");

printf ("\nName:%s %s \n" ,name ,lastname);
printf ("Address:%s\n",add);
printf ("postal code:%s\n",post);
printf ("Town:%s\n",town);
printf ("State:%s\n",state);
printf ("Tel:%s\n",tel);

}
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • 3
    No I am sorry... Where is your question? – DotNetRussell Oct 09 '13 at 15:34
  • don't pass a reference into `printf` – clcto Oct 09 '13 at 15:35
  • What's your issue and what's your question ? – zakinster Oct 09 '13 at 15:35
  • Your question being too long is not the problem. Problem is that there is no question. – LihO Oct 09 '13 at 15:35
  • ***Yes***. **Yes**! Yes, you did mess up. – Marc Claesen Oct 09 '13 at 15:38
  • well,didn't see those comments coming,so sorry..I dont know how to properly address the problem.May i ask what do you mean by that @clcto? – Hafiz Syazwan Oct 09 '13 at 15:49
  • if you guys could help I would be really happy,thanks guys. – Hafiz Syazwan Oct 09 '13 at 16:07
  • What I think @clcto means is that all of your variables are strings (arrays of chars). With int, float, double et. al. you would use the ***&*** to pass the address of the variable, but with strings, you do not. the name of the string already points to the address needed. So, change `scanf ("%s",&name);` to `scanf ("%s",name);`. (same with the other lines) – ryyker Oct 09 '13 at 16:09
  • @ryyker so i've tried what you said,and the results still mixed,for example:my address has numbers and letters,and when I add space it will be pushed into "town:" or "post:" – Hafiz Syazwan Oct 09 '13 at 16:12
  • both numbers and letters from a users input using scanf with "%s" will all be scanned as char into a string. The other problem - of a line with an embedded space being scanned into two lines is addressed later in this post, see the comments following my answer below. – ryyker Oct 09 '13 at 18:35

3 Answers3

0

Try this (& all removed) See my comment above for reason.

#include <stdio.h>

int main ()

{

 char name[50] , lastname[50] , add[100], post[50], town[60], state[60], tel[50];

 printf ("**** PLEASE ENTER THIS CONFIDENTIAL INFORMATION****");

  printf ("\n=================================================\n=================================================");

printf ("\n\nFirst name:");
scanf ("%s",name);

printf ("\nLast name:");
scanf  ("%s",lastname);

printf ("\nAddress Please:");
scanf ("%s",add);

printf ("\nPostcode:");
scanf("%s",post);

printf ("\ntown:");
scanf ("%s",town);

printf("\nTelephone number:");
scanf("%s",tel);


printf ("\n\n****CONFIDENTIAL INFORMATION****");

printf ("\n=================================================\n=================================================");

printf ("\nName:%s %s \n" ,name ,lastname);
printf ("Address:%s\n",add);
printf ("postal code:%s\n",post);
printf ("Town:%s\n",town);
printf ("State:%s\n",state);
printf ("Tel:%s\n",tel);

}
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • thanks for the answer and for editing,but when I execute it for example my address is"357,jln batik 1/1" then the "jln" and "batik" skips to the variable "post",which confuses me.Maybe it's just my computer,maybe it's me – Hafiz Syazwan Oct 09 '13 at 16:23
  • The space in the address string is causing the string to be scanned by the address, then the postcode. ***[Look here](http://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf)*** for how to use scanf with embedded spaces. – ryyker Oct 09 '13 at 16:33
  • I don't quite understand the link you give yet,but I'll try my best to do so,thanks a lot @ryyker – Hafiz Syazwan Oct 09 '13 at 16:43
  • It is mainly pointing out another way to capture input from a user by using `fgets (name, MAX_NAME_SZ, stdin);` instead of `scanf ("%s",name);`. It provides more control over unexpected things like embedded spaces etc. Try the suggested technique there, and if you have questions, post them here. – ryyker Oct 09 '13 at 17:49
0

When you do scanf for strings, you don't need

scanf("%s",&str);

instead just do

scanf("%s",str).

Kraken
  • 23,393
  • 37
  • 102
  • 162
0
#include <stdio.h>
int main()
{
    int i=3;
    int *j;
    j = &i;

    printf("i %d\n",i);//value of i
    printf("j %d",*j);//value of i in j
    printf("j %d",&j);// address of i in j
   return 0;
}

So, this is how referencing and dereferencing a pointer is done. I just gave the clue. Now you use your brain... Cheers!

Jency
  • 269
  • 1
  • 13