cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
basic char vs string question
basic char vs string question
Sep 12, 2008 at 10:28pm UTC
califguy
(12)
#include<stdio.h>
#include<stdlib.h>
main()
{
char a[32],*a1,b[64],*b1;
a1=&a[0];
b1=&b[0];
a1="hellohellohellohellohellohello" ;
printf("\n contents of a1: %s\n",a1);
int i =0;
for(i=0;i<=31;i++)
{
printf("%c",a[i]);
}
}
So why does printf ("%s",a1) work correctly while printf("%c",a[i]) display junk ? Since a1 points to a[0], shouldn'it it display the same values ?
Sep 12, 2008 at 10:40pm UTC
guestgulkan
(2942)
So why does printf ("%s",a1) work correctly while printf("%c",a[i]) display junk ? Since a1 points to a[0], shouldn'it it display the same values ?
NO.
Have a
close
look at your code again......
Sep 12, 2008 at 10:47pm UTC
califguy
(12)
I printed out the address values of &a[0] and a1 and they match initially. but after a1 gets the "hello...." value, it changes its address value and points elsewhere.
Not sure I get it.
Sep 12, 2008 at 10:59pm UTC
califguy
(12)
strcpy(a,a1)
did it ;)
Thanks anyways.
Topic archived. No new replies allowed.