char*
and char[]
are different types, but it’s not immediately apparent in all cases. This is because arrays decay into pointers, meaning that if an expression of type char[]
is provided where one of type char*
is expected, the compiler automatically converts the array into a pointer to its first element.
Your example function printSomething
expects a pointer, so if you try to pass an array to it like this:
char s[10] = "hello"; printSomething(s);
The compiler pretends that you wrote this:
char s[10] = "hello"; printSomething(&s[0]);
Related Posts:
- char *array and char array[]
- C pointers and arrays: [Warning] assignment makes pointer from integer without a cast
- The difference between char * and char[] [duplicate]
- How do I determine the size of my array in C?
- Pointer Arithmetic
- How do I create an array of strings in C?
- “Expected expression before ‘ { ‘ token”
- How to print the array?
- How do you make an array of structs in C?
- How do I create an array of strings in C?
- Return char[]/string from a function [duplicate]
- Difference between char* and const char*?
- Why am I getting “array initializer must be an initializer list or string literal”?
- What does ** do in C language?
- #31 expression must have integral type
- How do you pass a function as a parameter in C?
- Invalid pointer error on invoking free() after malloc in C
- Using Dynamic Memory allocation for arrays
- expression must have integral type
- How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?
- Finding the length of a Character Array in C
- What’s wrong with my code? What is argv[1]?
- Return char[]/string from a function
- Valgrind: Invalid read of size 1
- How to print a char array in C through printf?
- How to clear all the elements of array in C?
- Constant pointer vs Pointer to constant
- Reversing a string in C
- Zero an array in C code
- Passing an array by reference in C?
- Excess elements in char array initializer error
- Warning: return from incompatible pointer type in C
- strcmp giving segmentation fault
- Why am I getting this error: “data definition has no type or storage class”?
- Initialization from incompatible pointer type warning when assigning to a pointer
- I’m getting “Invalid Initializer”, what am I doing wrong?
- Returning an array using C
- Convert char array to string use C
- warning: passing argument ’from incompatible pointer type [enabled by default]’
- Iterate through a C array
- char pointers: invalid conversion from ‘char*’ to ‘char’?
- Excess elements of scalar initializer for pointer to array of ints
- typedef struct pointer definition
- Difference between char *argv[] and char **argv for the second argument to main()
- Warning: comparison of distinct pointer types
- Dereference void pointer
- Pointer to 2D arrays in C
- lvalue required as increment operand
- Implementing Taylor Series for sine and cosine in C
- pthread_join() and pthread_exit()
- Why should we typedef a struct so often in C?
- What exactly is the difference between “pass by reference” in C and in C++?
- What does “collect2: error: ld returned 1 exit status” mean?
- Floating point exception (core dumped)
- Undefined reference to pthread_create in Linux
- Correct format specifier for double in printf
- dereferencing pointer to incomplete type
- Get a substring of a char* [duplicate]
- Expression must be a modifiable L-value
- How do I use valgrind to find memory leaks?
- “error: assignment to expression with array type error” when I assign a struct field (C)
- Error “initializer element is not constant” when trying to initialize variable with const
- munmap_chunk(): invalid pointer
- What is the LD_PRELOAD trick?
- What is the argument for printf that formats a long?
- Cannot assign requested address – possible causes?
- Using %s in C correctly – very basic level
- What is the difference between const int*, const int * const, and int const *?
- How to create my own header file in c++?
- Is there a function to copy an array in C/C++?
- Returning string from C function
- Why do I get an assertion failure?
- Difference between int32, int, int32_t, int8 and int8_t
- Char Comparison in C
- Passing a 2D array to a C++ function
- what is the unsigned datatype?
- Reading a string with scanf
- getopt_long() — proper way to use it?
- C read file line by line
- C read file line by line
- Copying a part of a string (substring) in C
- Incompatible implicit declaration of built-in function ‘malloc’
- Why is %c used in C?
- expression must have integral type
- Need more information about Aborted (core dumped)
- When a number is written as 0x00… what does the x mean
- How to remove the character at a given index from a string in C?
- waitpid, wnohang, wuntraced. How do I use these
- Writing binary number system in C code
- How to convert const char* to char* in C?
- Scanning Multiple inputs from one line using scanf
- Why do I get “cast from pointer to integer of different size” error?
- C subscripted value is neither array nor pointer nor vector when assigning an array element value
- Which of sprintf/snprintf is more secure?
- How to allocate array of pointers for strings by malloc in C?
- Use of flag in c?
- What primitive data type is time_t? [duplicate]
- Using pointer to char array, values in that array can be accessed?
- Usage of \b and \r in C
- Parsing command-line arguments in C