By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,491 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

String pointers

P: 2
Hi,

Is it possible to assign string variables address to a string pointers ?

say that,

char a[20];
char *sptr[20];
strcpy(a,"apple");
sptr=&a;

or else,

scanf("%s",*sptr);

is this is possible?

then how do we assign one string variable address to string pointer?


regards,
vjee
Mar 22 '07 #1
Share this Question
Share on Google+
2 Replies


P: 1
Hi,

Is it possible to assign string variables address to a string pointers ?

say that,

char a[20];
char *sptr[20];
strcpy(a,"apple");
sptr=&a;

or else,

scanf("%s",*sptr);

is this is possible?

then how do we assign one string variable address to string pointer?


regards,
vjee
there is nothing like string pointers , that was array of pointers which u have created...
Mar 22 '07 #2

Expert 100+
P: 1,510
the name of an array, such as a[], is in effect a pointer to its first element so you don't need to put & in front of a in the assignment
Expand|Select|Wrap|Line Numbers
  1. sptr=&a;
in addition sptr is an array of char* and needs an index, e.g.
Expand|Select|Wrap|Line Numbers
  1.    char a[20];
  2. char *sptr[20];
  3. strcpy(a,"apple");
  4. sptr[0]=a;
  5. printf("%s", sptr[0]);
the statement
Expand|Select|Wrap|Line Numbers
  1. sptr[0]=a;
assigns the address of the first element of array a[] to the first element of array sptr[]
Mar 22 '07 #3

Post your reply

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