0

I want store values into a character pointer pointer.I have worked out a code which is mentioned below:

#include<string.h>
#include<stdlib.h>
#include<stdio.h>

int i=0,j,n,no_stops;
char *r[10],*s[10][10];
char *a,*b;


void fetch_routes_stops()
   {
   printf("Enter the no of routes:");
   scanf("%d",&n);
   for(i=0;i<n;i++)
   {
       printf("\nEnter the route No %d:",i+1);
       scanf("%s",&a);
       r[i]=strdup(a);
   }
}

void main()
{
    fetch_routes_stops();
    for(i=0;i<n;i++)
    {
        printf("\n%s",r[i]);
    }
}
4
  • void main() --> int main(void) Commented Jul 15, 2015 at 13:30
  • What are you trying to do? Commented Jul 15, 2015 at 13:31
  • Pick one of C++ and C, they are different languages. Commented Jul 15, 2015 at 13:35
  • c will be preferable Commented Jul 17, 2015 at 4:22

1 Answer 1

1

In your code,

 scanf("%s",&a);

is wrong, as a is uninitialised, and &a is not the argument you would need to pass. You need to allocate enough memory to a before passing that.

Better, you don't need to have a pointer for that itself. You can chnage your code like

  char a[128];

and then, use

  scanf("%127s", a);    //limit the input against overflow

to take the user input.

Or, if you insist to use the pointer and want to go for dynamic memory allocation, you can have a look at malloc() and family of functions. Remember, in that case also, you pass a to scanf(), not &a.

After that, please note

  1. The recommended signature of main() is int main(void).
  2. Do not use unnecessary global variables. You can always pass the variables as function parameters to use them across functions.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.