answersLogoWhite

0

How are character pointers handled in C?

Updated: 8/20/2019
User Avatar

Wiki User

11y ago

Best Answer

In C, pointers are addresses to specific locations in memory. They contain no additional information. It's up to the compiler, memory management system or programmer to keep track of how much space is being used by the program that allocates the memory.

C allows programmers to assign a type to the variable containing the pointer address. In the case of this question, the pointer variable references memory in one-byte chunks, each byte pertaining to a 8-bit signed integer called a "character", or char.

Also keep in mind that pointers are nothing more than glorified integers. In modern C Programming, these integers are 32-bit values. Since integers can be added to and subtracted from, pointers can as well, and the following code is perfectly legitimate:

#include

#include

void main()

{

char *mystr="Hello there!";

printf("ccc%c",

*mystr, *(mystr+4), *(mystr+3), *(mystr+2), *(mystr+8), *(mystr+9), *(mystr+strlen(mystr)-1));

}

By prefixing the pointer variable with an asterisk (*), the code is telling the compiler to access the value at the memory location specified by a variable by the type of the variable. *mystrrefers to the character located at the address of the literal string "Hello there!", which is an 'H'.

The above code is also referencing memory location offsets from the address in mystr. mystr+4 refers to the memory location 4 bytes past mystr's address, which is the 'o' in "Hello".

The last wonderfully messy parameter adds the length of the string data at mystr to the address of mystr and subtracts 1. Since strings in C are NUL-terminated (zero-terminated), adding the length of the string to the memory location of the string refers to the value 0 terminating the string. Subtracting 1 results in the last character of the string, which in this case is the exclamation mark.

All pointer variables can also point to memory addresses returned by the malloc() function. For instance, here's a simple string duplication function like strdup():

#include

char *stringdup(char *str)

{

char *returnstr;

returnstr=malloc(strlen(str)+1);

memcpy(returnstr, str, strlen(str)+1);

return returnstr;

}

Once the pointer returned from stringdup() is no longer needed, simply use free() to release the memory. This helps to avoid memory leaks in your program.

Note that, when or if you decide to begin exploring C++, you'll need to handle typecasting more intensively. C++ distinguishes between const char* and char*, for instance, and char* and void*. (malloc() returns a void*, so the stringdup() function above would produce a warning without typecasting the malloc() return value to char* when assigning it to returnstr.)

That's really all there is to know about character pointers in C.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How are character pointers handled in C?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Why pointer is callded jewel of c language?

a pointer is a derived data type in c. pointers are undoubtedly one of the most distinct and exciting features of c language.it has added power and flexibility to the language. *pointers are more efficient in handling arrays and tables. *pointer can be used to support dynamic memory management. *pointers reduce length and complexity of programs. *increase the execution speed and thus reduce the program execution time. by following character's real power of c lies in proper use of pointers. pointer is called the jewel of c-language.


What is the difference between c plus plus and java programming?

Java doesn't have pointers. C++ has pointers.


What is stream pointer in c?

C does not have stream pointers.


What is decleration part in C programming?

pointers.


What is the difference between pointers in c and c plus plus?

Nothing.


What is unmanaged heap?

The unmanaged heap is a region of memory allocated at runtime for applications in languages like C or C++. Developers are responsible for memory management, including allocation and deallocation, in the unmanaged heap. This can lead to issues like memory leaks or dangling pointers if not handled carefully.


Pointers are include in csharp or not?

Yes, you can use pointers in the C#, but to some extent. Links are added with more details.


What are the pointers in computer programming using c?

addresses


What is indirection in c?

Accessing data via pointers.


Why does everybody on this website who asks about C programming ask about pointers?

Not everybody asks about pointers, however pointers are a fundamental data type in C and can be difficult to understand when you come from a language that does not support a native pointer type. It is extremely low-level but in higher level languages, such as Java, pointers are abstracted away using references. Moreover, the close relationship between a pointer and an array in C means that it is vital we understand what a pointer is and why we need them.


Which are the possibe arithmetic operation with pointers?

Pointers in C are stored as integers. You can perform any mathematical operations on pointers that you can perform on ints.Of course not, the following operations are possible: =, +, +=, ++, -, -=, --, *, [], ->, typecast


In C you use the concept of pointers whereas there are no pointers used in JAVA why?

Pointers in C are generally the thing that gives learners the most trouble. When C code is not written correctly with respect to pointer use, the resulting bugs can often be very difficult to find and correct. On the other hand, pointers are absolutely necessary in some cases.The designers of Java wanted to make programming easier and hence avoided adding pointers to the language. Java does have object references which accomplish much of what pointers accomplish albeit in a safer way.