0

I am trying to write a program to access the name from user and print each of element and it address but can't figure out what happen to my code. If Gcc compiler not support Conio.h library so what should i do? Can I write a program without using conio.h library?. Please explain it

Here is my code:

//Write a program to find each of string element and it location
#include <stdio.h>
#include<conio.h>

int main(){
char name[10];
int i = 0;
printf("Please enter your name: ");
scanf("%s", name);
while(name[i] != '\0'){
    printf("%c is located at %u", name[i], &name[i]);
    i++;
}
getch();

return 0;
}

Output: No such file or directory

complination terminated

Majadul Haque
  • 49
  • 1
  • 8
  • Possible duplicate of [What is Equivalent to getch() & getche() in Linux?](https://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux) – Appleman1234 Oct 31 '18 at 05:33
  • First comment out conio.h. Then see which lines give you build errors, and fix them. Looks like it will be very easy in this case. – hyde Oct 31 '18 at 05:34
  • You are missing a space between #include and . I'm not sure if that's your only problem, but fix that and see what happens. – Leonard Oct 31 '18 at 05:34
  • Please understand that in this program `conio.h` is not needed for accepting the string. `scanf()` is defined in `stdio.h`. In this case, you are using `conio.h` to use `getch()`, which is commonly used to wait till user input before the program terminates. – Chris Aby Antony Oct 31 '18 at 05:36
  • I comment out conio.h but it give me an error in function getch().@hyde – Majadul Haque Oct 31 '18 at 05:39
  • remove those functions too as they are defined in conio.h – suvojit_007 Oct 31 '18 at 05:40
  • @Chris Aby Antony i already remove the conio.h and remove the getch() function but now it gives me an error. It said - format ‘%u’ expects argument of type ‘unsigned int’, but argument 3 has type ‘char *’ – Majadul Haque Oct 31 '18 at 05:45
  • @suvojit_007 i already remove thoose function which is related to conio.h. But give me an error in %u (unsigned operator).. What should i do now! – Majadul Haque Oct 31 '18 at 05:48

1 Answers1

0

You should actually display a pointer using the %p format specifier. For more information Correct format specifier to print pointer or address?

#include <stdio.h>

int main(){
  char name[10];
  int i = 0;
  printf("Please enter your name: ");
  scanf("%s", name);

  while(name[i] != '\0'){
     printf("%c is located at %p", name[i], &name[i]);
     i++;
}


  return 0;
}

Edit: As mentioned in the comment by @David, you should check the return value of scanf also.

if (scanf ("%9s", name) != 1){ 
   fputs ("error: name invalid or EOF\n", stderr); 
    return 1; }
suvojit_007
  • 1,690
  • 2
  • 17
  • 23
  • How about proper indention, protecting the bounds of `name` and checking the return of `scanf` to ensure `name` is a valid string before `printf`? `if (scanf ("%9s", name) != 1) { fputs ("error: name invalid or EOF\n", stderr); return 1; }` – David C. Rankin Oct 31 '18 at 05:59
  • @suvojit_007 Thanks a lot Brother. Have one more question ! If i want the index value of the string element rather than it address what should i need change? – Majadul Haque Oct 31 '18 at 06:01
  • @TusarHaque use `%d` for index value as they are integer – suvojit_007 Oct 31 '18 at 06:03
  • @DavidC.Rankin thanks for pointing that out. I have added that information. – suvojit_007 Oct 31 '18 at 06:08
  • @TusarHaque Please close the question, if you don't have any more issues. – suvojit_007 Oct 31 '18 at 06:10
  • @suvojit_007 %d give an error! ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘char *’. But I want to use the char data type and get the output like first letter located at 0, 2nd at 1.. what should i need to change! – Majadul Haque Oct 31 '18 at 06:31
  • @TusarHaque use `printf("%c is located at %d with address %p", name[i],i &name[i]);` – suvojit_007 Oct 31 '18 at 06:34