answersLogoWhite

0


Best Answer

The difference here is that char *p = "Hello";

will place Hello world in the read-only parts of the memory and making p a pointer to that, making any writing operation on this memory illegal. While doing: char p[] = "Hello";

puts the literal string in read-only memory and copies the string to newly allocated memory on the stack.

p[0] = 'A'; is legal.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between char pointer and char buffer?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you use a pointer?

A pointer is a reference to a location in memory that is a primitive type in C and other low-level languages. An example use would be if you wanted to store data at run-time, you would have to allocate memory using the malloc function, an you would access that memory area using a pointer Example (modified from http://www.cplusplus.com/reference/clibrary/cstdlib/malloc): #include <stdio.h> #include <stdlib.h> int main () { int i,n; char * buffer; // Declare a pointer to a char printf ("How long do you want the string? "); scanf ("%d", &i); // Get input from user and store in i // Allocate space for i+1 characters buffer = (char*) malloc ( sizeof( char[i+1] ) ); // buffer should now contains a pointer to this location if (buffer==NULL) exit (1); // Memory allocation failed, abort for (n=0; n<i; n++) buffer[n]=rand()%26+'a'; buffer[i]='\0'; printf ("Random string: %s\n",buffer); free (buffer); // Release allocated memory return 0; }


Difference between void pointer and null pointer?

A Null pointer has the value 0. void pointer is a generic pointer introduced by ANSI. Before ANSI, char pointers are used as generic pointer. Generic pointer can hold the address of any data type. Pointers point to a memory address, and data can be stored at that address.


What is the difference between premitive and non premitive in operating system?

premitve is a int char float , pointer.... n non premitive is a arrays, structure, union


What is the difference between tar and char?

The spelling!


Why gets function gives a warning every time you compile a c program?

It is unsafe. In order to use gets() safely, you need to know how many characters you will be reading to ensure your character buffer is large enough: char buffer[10]; while (gets (buffer) != 0) { ...process buffer... } The above code has undefined behaviour when the number of characters read is 10 or more (you need one character for the null-terminator). This is because the character buffer, str, decays to a pointer (referencing &str[0]) and the function, gets(), cannot determine the number of characters in a buffer by its pointer alone. The gets() function was dropped from the C standard in 2011, however some implementations still include it. To avoid the warning, use the fgets() function instead. This allows you to specify the length of your buffer and (when used correctly) prevents buffer overflow. char buffer[10]; while (fgets (buffer, 10, stdin) != 0) { ...process buffer... }


Difference between char and varchar datatypes?

Char is fixed length, while Varchar is variable length.


Can a pointer be considered a variable?

You can declare pointer-variables, if that's what you mean. Example: char *sample = "Sample";


How do you declare a pointer to a character string in c?

char *ptr;


Pointer version of string function strcpy?

char* strcpy(const char* src, char* dst) { char* tmp = dst; while ((*dst++ = *src++) != '\0'); return tmp; }


What is declaring pointer variable?

I'll give you an example: char *s;


What is suspecious pointer conversion error?

Assignments between different types of pointer. Example: int i, *ip; char c, *cp; ip= &c; ip= cp; cp= &i; ip= cp; /* all wrong */


Can you assigned value of int to a ordinary pointer?

Yes, with type-cast (but I don't see why you should): char *ptr = (char *)300;