All Questions
296 questions
0
votes
2
answers
128
views
GCC compiler handles "char * str" and "char str[]" with the & operator differently [duplicate]
The compiler treats the types char * var and char var[] differently when it comes to taking the address of a variable via the & operator. Here's a little snippet of code to demonstrate:
#include &...
1
vote
0
answers
96
views
Invalid pointer own get_next_line implementation
In the last reading of the file, when it still has to return a line, and the line variable has the content of that line, an invalid pointer error occurs when doing free in the main but that line has ...
1
vote
2
answers
173
views
Incompatible pointer type assignment for double pointer to constant double pointer
I have the following code:
typedef struct S1 S1_t;
struct S1
{
uint8_t *ptr1;
uint8_t **ptr2;
};
void get_ptr1(const S1_t *s1, uint8_t const **ptr1)
{
*ptr1= s1->ptr1;
}
void get_ptr2(const ...
3
votes
2
answers
270
views
Pointer of number in C
Assume there's function that get int * parameter.
void foo(int *x)
{
}
If I want to call this function without creating an int variable
int main()
{
foo(&1);
return 0;
}
compilation ...
0
votes
1
answer
56
views
Passing by pointer a struct instance initialized within one function on to another function in C yields different results using GCC
(I'm doing this for a personal project - just to learn how things like pointers/references/memory management work with C language. I want to do this "without cheating", i.e. without any aid ...
1
vote
1
answer
123
views
Are there any way to tell if comparison between C constant pointers are made in GCC and Clang without manual static analysis?
This is to see what lines of code are affected by -fmerge-all-constants.
For example:
// main.c
#include <assert.h>
#include <stdio.h>
int foo(const int* p){
const int c[2] = {68,41};...
3
votes
1
answer
88
views
Computing the address of the memory position before an external symbol in C
In my C code I have an external symbol, some_symbol. I need to get the address of the memory position just preceding that symbol (&some_symbol-1). This used to work fine in older versions of gcc, ...
-2
votes
1
answer
100
views
format %f expects argument of type 'float * ' , but argument 2 has type "double" [-Wformat=]
#include <stdio.h>
#define MAX_NAME 10
#define MAX_PORTFOLIO 1000
typedef struct {
char nome[MAX_NAME];
double valor;
double yield;
} stock_t;
int main(){
int i,n,k;
float ...
1
vote
3
answers
95
views
How to not "return address of local variable" when modifying a copy of a struct?
I have the struct:
struct mystruct {
int a;
};
If I create a function with the struct as an argument,
and try to directly return its address:
struct mystruct *
modifystruct1(struct mystruct s)...
0
votes
2
answers
388
views
Why I am not receiving a hexadecimal number as output when I print a pointer address with %p?
I have written a program while learning pointers in c and facing a problem. My code was to print the address of the variable which should be a hexadecimal number. But why I am receiving an integer ...
0
votes
1
answer
1k
views
Why is there a `-Wint-conversion` warning?
I'm not new to C, but this doesn't make sense in my mind. in my char encrypt function, I get this warning:
crypt.h: In function ‘encrypt’:
crypt.h:32:9: warning: returning ‘char *’ from a function ...
0
votes
0
answers
40
views
running external program and feeding its outputs as inputs to variables
In the following code, rval3 is hardcoded and I do some manipulation to get
the rev. Which does what I expect.
int rval3= 0x0c10; // want to make this generic
int rval3= *buf;
int rev;
...
0
votes
1
answer
87
views
C function that executes some code when the function that called it exits
I want a function which achieves something like the behavior shown in the pseudo C code listed.
I figure that this might be possible via the use of function pointers?
If this pattern I've dreamed up ...
0
votes
1
answer
61
views
How to pass different structure in a single function as argument
I have 2 structure name struct1, struct2. also i have one manipulation function named "myFun"
void myFun(/one pointer argument/)
i have a set flag , if set flag is 1 i need to pass struct1 ...
1
vote
1
answer
66
views
Dereferencing type-punned pointer will break strict-aliasing rules while compiling cannot be fixed to compile C code
I try to convert C Code from Standard 11 to 14. I work with gcc 7.5.0 in Ubuntu 20.04. I have the following lines of code
json.h:
extern const struct _json_value json_value_none;
typedef struct ...
0
votes
1
answer
274
views
Why strrev() in C is not working with char*?
Using C programming, with GCC in windows (with CodeBlocks)
Why this code doesn't work (running it gives empty console window)
# include <stdio.h>
# include <string.h>
int main()
{
char* ...
0
votes
1
answer
139
views
Assign function with const argument to function pointer with non const argument
Note: I have seen assigning-function-to-function-pointer-const-argument-correctness. While it applies to C++, the accepted answer seems to hint that this shouldn't be an issue.
I have compiled both ...
0
votes
1
answer
69
views
Understanding Pointers and Scope in C With Different Compilers
I have recently encountered an issue with how gcc compilers handle a pointer in some legacy code. Taking the below as a quite cut down example of how the code is set up:
int someCondition; // Set ...
0
votes
1
answer
42
views
How do i assign pointer value inside array of pointers
i have this error:
ID3.c:71:53: error: incompatible types when assigning to type ‘t_object’ {aka ‘struct object’} from type ‘t_object *’ {aka ‘struct object *’}
71 | (objects->v[...
2
votes
1
answer
207
views
Why is GCC 11 compiler producing weird output when optimization is enabled?
Please take a look at this code:
#include <stdio.h>
#include <stdint.h>
int main()
{
uint8_t run = 1;
/* Define variable of interest and set to random value */
uint32_t ...
1
vote
1
answer
38
views
Compilation error for defining static array with pointers
When I define an array like
double *first = (double *)malloc( N*N*sizeof( double ) );
There is not problem. But when I specify
static double *first = (double *)malloc( N*N*sizeof( double ) );
I get ...
0
votes
3
answers
132
views
Initialising a C vector pointer throws a bus error but initialising a vector doesn't
This C code:
#include <stdlib.h>
typedef struct
{
int *a;
int sz, cap;
} Vector;
void vector_init(Vector *v)
{
v->a = (int *)malloc(sizeof(int));
v->sz = 0;
v->cap ...
5
votes
1
answer
254
views
Is this write to an array truly undefined behavior in C? [duplicate]
This code writes a value through a pointer if one array is one past the end of another array.
#include <stdio.h>
#include <inttypes.h>
extern int first[], second[];
#define ADDR_AFTER(...
0
votes
1
answer
425
views
Segmentation fault when returning pointers [duplicate]
I recently started learning C, and an issue came up with this code:
#include <stdio.h>
#include <stdlib.h>
int* add(int* a,int* b)
{
//a and b are pointers to integers
int ...
0
votes
0
answers
43
views
`ld` says it places a variable somewhere, but places it elsewhere
For an embedded application, I have a certain section in program memory, which I would like to hold a collection of state variables.
I want this section to be called ".mysect", and be placed ...
2
votes
0
answers
439
views
Multi dimensional array as VLA function parameter with explicit size
Context
I am working on a computational fluid dynamics code where I work with vector and matrices. Consequently I want to pass them to functions every once in a while.
The memory for vectors and ...
0
votes
1
answer
607
views
GCC Warning: "Initialization from incompatible pointer type [enabled by default]"
'''
bool loadArray(unsigned int array[8][8]) { int* p = array; ... }
'''
I have a function that loads array[8][8] with user input. Everything works fine, but trying to compile it with gcc (c99) it ...
5
votes
3
answers
573
views
Why do gcc and clang not warn about writing to address 0?
The following erroneous code:
#include <stdio.h>
#include <string.h>
void isEven (int *isFlag, int num) {
if (num % 2 == 0) {
*isFlag = 1;
} else {
*isFlag = 0;
...
0
votes
1
answer
182
views
Initialize multi-level pointer to pointer and compile error occurs
I have multi-level pointer to pointer code like this:
int
main(int argc, char **argv) {
int a = 1;
int *pa = &a;
//int **ppa = &pa; // [Right Code]
int **ppa = &...
0
votes
0
answers
137
views
Detecting if a macro parameter is struct/direct type or pointer in C – what's the GCC builtin for this?
In order to differentiate between non-pointers and pointers at compile time one can use macro as:
#define IS_STRUCT_SUBSTANTIAL(x) (sizeof(x) != sizeof(void *))
However it will fail for 8 byte ...
1
vote
1
answer
956
views
How to store and use a value again in C
I want to store a value value via scanf() in the function lorem(), and use it in the function ipsum(). I used static int, but it doesn't work.
static int *value;
void lorem(){
static int *value;
...
0
votes
2
answers
351
views
Why does writing to an uninitialized pointer not raise a warning?
Why is it that I'm able to do the following?
char *name;
scanf("%10s", name);
printf("%s\n", name);
And the program/compiler doesn't raise a warning? I thought that since the name ...
0
votes
0
answers
36
views
Why a string declared between quotation marks seems to be sometimes mutable, sometimes immutable? [duplicate]
I know that when declaring char * s = "arbacadabra";, "abracadabra" refers to a const char *, but I cannot understand why when declaring t[] = "abracadabra", "...
-1
votes
7
answers
395
views
Why do function pointers exist as long as they act similar to functions themselves?
I know that there is a similar topic, but those answers don't clarify what I want to find out. Thus, as long as from the excerpt below one can notice that a function and the reference to that function ...
0
votes
3
answers
425
views
Why are pointer type casts necessary?
I cannot understand why are pointer type casts necessary, as long as pointers point to an address and their type is important only when it comes to pointer arithmetic.
That is to say, if I encounter ...
16
votes
1
answer
400
views
gcc Strange -O0 code generation. Simple malloc. Pointer to multidimensional array
Very simple code:
void *allocateMemory5DArray(size_t x, size_t y, size_t z, size_t q, size_t r)
{
int (*array)[x][y][z][q][r];
array = malloc(sizeof(*array));
return array;
}
The -O0 gcc ...
0
votes
1
answer
58
views
Is it possible to return an address of a const pointer argument in a function without const qualifier without a warning while -Wall is activated?
Well, the question is mostly above. The only things I could add are that I've tried str+i and &(str[i]) and both gave me a discards 'const' qualifier from pointer target type warning.
Or is there ...
2
votes
1
answer
79
views
How do you store duplicate constant strings in a single pointer while still being able to know its length at compile time?
I would like to find a way to store duplicate constant strings in a single location. However; I need to get the length of that string at the compiler level (such that it is not found at runtime by ...
0
votes
1
answer
739
views
Incompatible function pointer types when making array of pointers
I'm working on creating a basic shell in C, running into issues when compiling with gcc:
This is the warning from my IDE:
[query] incompatible function pointer types initializing 'const void (*)(char *...
0
votes
3
answers
294
views
How can I duplicate an array of pointers in C? (uint32_t)
I have the following variable "places" which is a ptr to a dynamic array of string locations:
uint32_t* places;
I would like for my function to return a DUPLICATE pointer of type (uint32_t*)
...
1
vote
2
answers
86
views
Cutting a string when a specific character is found
I am making a function that "cuts" a string when it finds a certain character but im getting some king of bug and i cant find what is failing. The code:
#include <stdio.h>
#include <...
0
votes
3
answers
49
views
Referencing a property of a double pointer in c
A function in C that I wrote works like this:
int filepath_from_request(obj **context) {
strncpy(*context->request_filepath, *context->request, 1024));
}
The obj object itself is defined ...
0
votes
2
answers
36
views
Passing structs to a function using the pointer operator in C
In this code:
the struct I have has 2 members and with it, 3 variables are defined.
the values of two of them are assigned by me and the third one should come from a function.
the code:
#include <...
0
votes
1
answer
85
views
Why does a segmentation fault happen when i declare an integer pointer p and assign it to the return value of my function? [duplicate]
My function also returns an integer pointer. I was under the impression that array names are just integer pointers, so that's why i declared it like that. I have made a function called reverse() to ...
-1
votes
2
answers
71
views
why am i getting segmentation fault when i run this code?
#include <stdio.h>
void avg_sum(double a[], int n, double *avg, double *sum)
{
int i;
sum = 0;
printf("%f", *sum);
for(i=0; i<n; i++)
*sum += a[i];
*avg ...
0
votes
2
answers
109
views
Why is this kernel causing problems?
I decided to try and write a C Hello world protected mode Kernel, and although directly accessing the video memory works to print characters manually, I decided to try to write a string, and there are ...
1
vote
1
answer
101
views
What does output indicate when larger address is subtracted from smaller address in C/C++ programming?
Given a snippet of code as:
int a[5];
printf("%u\n",&a[3]-&a[0]);
printf("%u",&a[0]-&a[3]);
now the output of first line in subtraction of addresses is as per ...
1
vote
1
answer
1k
views
Understanding pointers in assembler from machine's view
Here is a basic program I written on the godbolt compiler, and it's as simple as:
#include<stdio.h>
void main()
{
int a = 10;
int *p = &a;
printf("%d", *p);
}
The ...
1
vote
1
answer
145
views
How to visualize the pointers in the code snippet?
I am finding it difficult to visualize this piece of code. I cannot seem to find the ans to this.
I did get the ans for
printf("**r = %d\n",**r);
printf("**s = %d\n",**s);
but other variables are ...
1
vote
2
answers
348
views
What makes the difference between direct and indirect access to volatile objects in C?
I am dealing with hardware registers on an STM32 controller.
I have defined a bunch of structures like the following:
#define PACKED __attribute__ ((packed))
#define ASSERT(cond) _Static_assert(cond,...