| You are here: | About>Computing & Technology>C / C++ / C#> C> C Tutorials> Assigning Strings using Pointers. |
![]() | C / C++ / C# |
Elsewhere on the WebC Tutorial - Lesson Three - About CharsAssigning Strings using Pointers.Let me now introduce pointers. This is one of the most complex things to learn in C and the subject of a later lesson to itself.
A pointer holds the address of a variable. When your application is loaded into ram part of the ram holds the variables. On the previous page we used the strcpy() function to assign a string. Another way to do this is with a pointer by using * after the type.
#include <stdio.h> The line char * name="David Bolton"; defines name as a pointer to the first character in the string. The assignment means that as soon as the program loads, the pointer is set to hold the address of where the string is stored. By adding these two lines after the printf, and making the example, you can see this. printf("The address of name is %x\r\n",(int)&name) ;%x prints the addresses in hexadecimal. The \r\n outputs a Carriage Return and a Linefeed character. (int)&name has two important parts. (int) converts (it's called a cast) the pointer type to an int. &name means the address of the variable name. When this runs it prints the following. On your computer the two addresses may be different. My name is David BoltonOn the next page : More about Pointers.
Elsewhere on the Web |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | Print this Page | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


