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.
The spelling!
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... }
char *ptr;
char* strcpy(const char* src, char* dst) { char* tmp = dst; while ((*dst++ = *src++) != '\0'); return tmp; }
Yes, with type-cast (but I don't see why you should): char *ptr = (char *)300;
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; }
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.
premitve is a int char float , pointer.... n non premitive is a arrays, structure, union
The spelling!
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... }
Char is fixed length, while Varchar is variable length.
You can declare pointer-variables, if that's what you mean. Example: char *sample = "Sample";
char *ptr;
char* strcpy(const char* src, char* dst) { char* tmp = dst; while ((*dst++ = *src++) != '\0'); return tmp; }
I'll give you an example: char *s;
Assignments between different types of pointer. Example: int i, *ip; char c, *cp; ip= &c; ip= cp; cp= &i; ip= cp; /* all wrong */
Yes, with type-cast (but I don't see why you should): char *ptr = (char *)300;