In this case a[4]
is the 5th
integer in the array a
, ap
is a pointer to integer, so you are assigning an integer to a pointer and that’s the warning.
So ap
now holds 45
and when you try to de-reference it (by doing *ap
) you are trying to access a memory at address 45, which is an invalid address, so your program crashes.
You should do ap = &(a[4]);
or ap = a + 4;
In c
array names decays to pointer, so a
points to the 1st element of the array.
In this way, a
is equivalent to &(a[0])
.
Related Posts:
- char *array and char array[]
- What is the difference between char array and char pointer in C?
- 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?
- What does “control reaches end of non-void function” mean?
- 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
- Variable warning set but not used
- 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
- What causes a segmentation fault (core dump) to occur in C?
- How to use symbols of extended ASCII table in C?
- Arrow operator (->) usage in C
- typedef struct vs struct definitions [duplicate]
- Why am I getting “void value not ignored as it ought to be”?
- Return array in a function
- 1 = false and 0 = true?
- Get a substring of a char*
- What causes the Broken Pipe Error?
- Strings and character with printf
- Why does the terminal show “^[[A” “^[[B” “^[[C” “^[[D” when pressing the arrow keys in Ubuntu?
- Why is the gets function so dangerous that it should not be used?
- What does “collect2: error: ld returned 1 exit status” mean?
- Warning comparison between pointer and integer
- Using ssize_t vs int
- Why a warning of “control reaches end of non-void function” for the main function?
- error: called object is not a function or function pointer
- C read file line by line
- where does stdio.o live in linux machine?
- conflicting types error when compiling c program using gcc
- Get the current time in C
- What is a file with extension .a?
- error: indirection requires pointer operand (‘int’ invalid)
- C++: Expression must have a constant value when declaring array inside function
- Encrypt with private key and decrypt with public key
- What does “pointer being freed was not allocated” mean exactly?
- Typedef function pointer?
- How to use timer in C?
- Convert char array to a int number in C
- What is the difference between stdin and STDIN_FILENO?
- Allocating string with malloc
- How to simply convert a float to a string in c?
- How to copy a char array in C?
- hash function for string
- Why use pointers?
- What happens during a “relocation has invalid symbol index” error?
- Initializing array of structures
- Double pointer array in c++
- Can I create an Array of Char pointers in C?
- What exactly is meant by “de-referencing a NULL pointer”?
- Error: “Access not within mapped region at address” (Valgrind)
- How to convert string to float?
- C char array initialization
- How does the fscanf function work?
- char array not assignable
- getline() vs. fgets(): Control memory allocation
- Segmentation fault- strcat
- C: warning: excess elements in array initializer; near initialization for ‘xxx’ ; expects ‘char *’, but has type ‘int’
- Initializing 2D char array in C
- Array of Linked Lists C++