score:3
they accomplish the same thing. buf
evaluates to a pointer to the first element (int*
) of the array, while &buf
gets you a pointer-to-array-of-8-int*
s. (the types differ in a somewhat obscure way.) both of those pointers can be used to access the byte representation of the array via casting to char*
, which is what memset()
conceptually does. internally, you're just passing the same address in both cases in practice.
as a pedantic note, it's not guaranteed that the internal representation of a null
pointer is as all-zero bytes (though this is extremely likely in practice).
to directly declare a variable of type pointer-to-array-of-8-int*
s, you could use the following by the way:
int* buf[8];
int* (*bufp)[8] = &buf; /* initialize a pointer-to-array-of-8-int* */
bufp
is different from a pointer to the first element of the array in that you can do e.g. sizeof *bufp
to get the size of the array. (that information would not be available to the compiler with just a pointer to the first element. the above makes it inherent in the type.)
as seen above, pointers and arrays aren't the same thing (they have different types, and there are dedicated types for arrays). the reason people often confuse pointers and arrays is that arrays usually evaluate to a pointer to their first element. sizeof
is one case where that does not happen. if the array degraded to a pointer with sizeof
, then you'd get the size of that pointer instead of the size of the array, which wouldn't be useful.
Source: stackoverflow.com
Related Query
- c memset function puzzle
- memcpy and memset function to java
- Macro definition for memset function in C on Mac OS X
- Memset function in programm that resolves host adresses
- Memset function in C initialised all the arrays
- The memset function is giving me an error when using VS C++
- Why should a non-void C empty function without return statement copy a struct?
- passing a struct argument in a struct function
- Achieving Object-Oriented Design in the C Language by using proper function names
- Why is it allowed to redeclare a static function as extern?
- Variadic Macro for custom print function with argument expansion in C
- Macro shadowing both function and its address
- Function is returning 1 instead of a value?
- Why don't I get a "conflicting types" error when function has no arguments?
- strlen function still work without including header file
- Different output on changing the position of fgets() function
- Should clang and gcc produce a diagnostic message when a program does pointer arithmetic on a function pointer?
- Inserting a new node to a singly linked list doesnt work when i call the function in a loop
- Best way to structure C code where malloc used inside a function but free in calling function
- Values of char* parameter changing inside function
- How to calculate the size of the statically allocated string in the calling function in C?
- Can a C function declaration have more than one identifier?
- Is there a way club two Linux command together in system() function in C?
- Segmentation fault (core dumped) after calling vfprintf function
- Why does this factorial function break after a certain point?
- How to call function from external library in C/C++
- error: expression must have struct or union type in c when passing struct as param in function
- The function must count how many blocks are entered
- Passing a matrix of strings to a function that modifies it
- How to get only the return value of a function in c?
More Query from same tag
- (C code) how would I pass my global variables between functions and return them when the main function needs them also?
- Using scanf in vscode (c language)
- elements in C array strange change
- WebAssembly: (f32.const nan:0x200000) means 0x7fa00000 or 0x7fe00000
- When redirecting printf to UART, output lines have a stair-stepped appearance
- C how to base structs off of struct
- PC-Lint Exclude External folder
- How to scan strings from a text file with delimiter and then display it?
- In C/C++, I am trying to call different functions (explained in description) in a for loop without if-else ladder
- Strange output at after copy an array of char from a pointer to a line file
- Pass binary data from C to C# through the console
- io_submit() blocks until a previous operation will be completed
- Error while reading an XML and validating the schema with XMLReader in PHP
- Get the greater of two numbers with bitwise operations
- How to include custom C library
- What is a "scan" in VGA programming?
- Binary Trees -- Tracing through code
- how to simplify the following code by using macro
- How to initialize and manipulate an address pointer
- value over written in the array during loop
- Sprintf_s giving me weird boxes instead of strings
- flatcc union as top element
- Is this a correct pcap filter expression
- gmake (Gnu) to make (Cygwin) - verification of my implementation [only 3 statements]
- Euler Totient: Optimization
- Compiler Warn on enum
- What does "code" mean in this context?
- Error while traversing through a char array to find numeric values
- API struct utilities - functions or macros?
- Pointer pointing to itself