I'm pretty new to the C. I'm trying to create a simple program to represent a point using a structure. It looks like this:
// including standard libraries
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
// including user defined libraries
;
typedef struct point {
char p_name;
double *p_coords;
} point_t;
int main() {
point_t *pt;
pt->p_name = "A";
printf("%c", pt->p_name);
// returning 0 if there are no errors
return 0;
}
The problem is that, when I try to print the name of the point after I assigned the name "A" to it, the program does output nothing except for the exit code, which is (probably) a random number:
Process finished with exit code -1073741819 (0xC0000005)
The fact is that pointers is a concept that is very hard for me to understand (I used to program in python before) and therefore I'm probably missing something. I've also tried out with other variable types such as int, but the result is the same (even the exit status number is the same). Is there a way to fix this behaviour?
P.S.: Excuse my rudimental English, I'm still practising it, and thanks a lot for your time!
In your code
pt->p_name = "A";
is wrong for two primary reasons:
You never made pt point to any valid memory location. Attempt to dereference an invalid memory invokes undefined behavior.
p_name is of type char. "A" is a string literal, of type char [x], which boils down to char * for assignment, and they are not compatible types.
You need to
Make sure pt points to valid memory location. Actually, you don;t need a pointer here, at all. Define pt as a variable (not a pointer variable) of the structure type, and access the members via the . operator.
Use 'A' for assignment, as in character constant, not a string.
Pointers must be made to point somewhere. You never assign a value to the pointer pt, and attempting to dereference an uninitialized pointer value invokes undefined behavior.
You're also assigning a string to a character value. String use double quotes while single characters use single quotes
Use single quotes for a character, and a pointer must first be made to point somewhere:
point_t p;
point_t *pt = &p;
pt->p_name = 'A';
printf("%c", pt->p_name);
you have to use malloc to allocate the memory of the poin_t structure.
Something like
point_t *pt = malloc(sizeof(point_t));
pt->p_name = 'A';
printf("%c", pt->p_name);
And very importantly as others mentioned is that pt->p_name = "a" is also wrong you are allocating int a char a const char* I fixed in my example
Related
Edit:
(1) Title (- previous title: How can I assign the address of a pointer to an already-existing variable? -- resolve at bottom of message);
(2) 'In short';
(3) spelling / punctuation.
In short: I am trying create and then locate a struct on the heap (I want to save memory on the stack), and passing arguments into various functions to populate the struct. In previous projects I created a pointer to struct, allocated this on the heap using malloc, and finally passing the pointer as argument to functions - this worked perfectly. My question: can the same be done without the use of a pointer?
I am trying to store a struct in dynamic memory. I succeeded in a previous mini-project, but I used pointer-to-struct, and passed this pointer to all my functions. Now I am burning to know if I could simply omit passing the pointer and pass the variable struct itself into the function.
My current example
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
typedef struct s_text t_text;
typedef struct s_text
{
int letters;
// some more stuff
} t_text;
int main(void)
{
t_text text;
t_text *tp;
tp = malloc(sizeof(t_text));
//&text = tp; <-- this here I tried, but error (value required as left operand of assignment)
return (0);
}
In the above code I allocate memory on the heap for the tp. This is the memory I'd like to use.
Now, on the stack, memory was reserved for (t_text) text. I would like to discard this and only use the heap.
t_text &text = malloc(sizeof(t_text)); <-- this may work in C++, i don't know, but in C definitely not.
In another post's discussion on NULL pointers, someone claimed in C++ that the address of a variable could point to NULL with the following code
int &x = *(int*)0;
but this definitely is not appreciated by my compiler. In fact, I tried several things with the address of a variable, but each time I try to set eg &text = (some address) this error pops up:
error: lvalue required as left operand of assignment.
(link to the post I refered to: ttps://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable/57492#57492 )
Below what I tried earlier (and works perfectly):
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
typedef struct s_text t_text;
void fn_prompt_user(t_text *tp);
void fn_calc_letters(t_text *tp);
typedef struct s_text
{
int letters;
// some more stuff
} t_text;
int main(void)
{
t_text *tp;
tp = malloc(sizeof(t_text));
fn_prompt_user(tp);
fn_calc_letters(tp);
return (0);
}
To conclude this post with my question: Is there a way I can pass a struct variable
as an argument to a function, or should I just accept passing pointer-to-struct is the one and only way to go?
Thanks!
-- answer to previous title's question (How can I assign the address of a pointer to an already-existing variable?): Not possible.
Error: error: lvalue required as left operand of assignment.
When declaring a variable, it is placed in memory. This memory location can not be changed, and so if int a = 3; a is an lvalue (location value) which can be changed (to eg. 4), but &a is unchangeable, therefor an rvalue (so is 3). So &a = ptr_a; will never work. Thanks for the clarification.
You can pass a struct to a function, like ....
int myfunc(t_text mytext) {....}
then ...
t_text thistext;
...
myfunc(thistext);
and this puts the entire struct onto the stack for the subroutine to use.
but the C language has no 'ref' feature like C++.
You can ...
tp = (t_text *)malloc(sizeof(t_text));
myfunc(*tp);
==
Your second example, passing pointers to objects, is a very conventional means of using structs in C. It has the advantage of not needlessly copying structs to the stack, merely pointers. It has the disadvantage of allowing functions to modify the objects that are pointed to. The latter problem can be remedied by declaring that the argument points to a const struct. Like:
void fn_promt_user(const t_text *tp) {...}
should I just accept passing pointer-to-struct is the one and only way to go?
Basically, yes.
C does not have "pass by reference" built into the language. If you want to have a function populate or otherwise modify a struct for you (or any other object for that matter), passing a pointer is the normal and idiomatic way of doing that. There is no real alternative, short of ugly macro hacks and stuff like that.
I'm dealing with pointers, double-pointers and arrays, and I think I'm messing up a bit my mind. I've been reading about it, but my particular user-case is messing me up, and I'd appreciate if someone could clear a bit my mind. This is a small piece of code I've built to show my misunderstanding:
#include <stdio.h>
#include <stdint.h>
void fnFindValue_vo(uint8_t *vF_pu8Msg, uint8_t vF_u8Length, uint8_t **vF_ppu8Match, uint8_t vF_u8Value)
{
for(int i=0; i<vF_u8Length; i++)
{
if(vF_u8Value == vF_pu8Msg[i])
{
*vF_ppu8Match = &vF_pu8Msg[i];
break;
}
}
}
int main()
{
uint8_t u8Array[]={0,0,0,1,2,4,8,16,32,64};
uint8_t *pu8Reference = &u8Array[3];
/*
* Purpose: Find the index of a value in u8Array from a reference
* Reference: First non-zero value
* Condition: using the function with those input arguments
*/
// WAY 1
uint8_t *pu8P2 = &u8Array[0];
uint8_t **ppu8P2 = &pu8P2;
fnFindValue_vo(u8Array,10,ppu8P2,16); // Should be diff=4
uint8_t u8Diff1 = *ppu8P2 - pu8Reference;
printf("Diff1: %u\n", u8Diff1);
// WAY 2
uint8_t* ppu8Pos; // Why this does not need to be initialized and ppu8P2 yes
fnFindValue_vo(u8Array,10,&ppu8Pos,64); // Should be diff=6
uint8_t u8Diff2 = ppu8Pos - pu8Reference;
printf("Diff2: %u\n", u8Diff2);
}
Suppose the function fnFindValue_vo and its arguments cannot be changed. So my purpose is to find the relative index of a value in the array taking as reference the first non-zero value (no need to find it, can be hard-coded).
In the first way, I've done it following my logic and understanding of the pointers. So I have *pu8P2 that contains the address of the first member of u8Array, and **ppu8P2 containing the address of pu8P2. So after calling the funcion, I just need to substract the pointers 'pointing' to u8Array to get the relative index.
Anyway, I tried another method. I just created a pointer, and passed it's address, without initializing the pointer, to the funcion. So later I just need to substract those two pointers and I get also the relative index.
My confusion comes with this second method.
Why ppu8Pos does not have to be initialized, and ppu8P2 yes? I.e. Why couldn't I declare it as uint8_t **ppu8P2;? (it gives me Segmentation fault).
Which of the two methods is more practical/better practice for coding?
Why is it possible to give the address to a pointer when the function's argument is a double pointer?
Why ppu8Pos does not have to be initialized, and ppu8P2 yes
You are not using the value of ppu8Pos right away. Instead, you pass its address to another function, where it gets assigned by-reference. On the other hand, ppu8P2 is the address of ppu8Pos you pass to another function, where its value is used, so you need to initialise it.
Which of the two methods is more practical/better practice for coding
They are identical for all intents and purposes, for exactly the same reason these two fragments are identical:
// 1
double t = sin(x)/cos(x);
// 2
double s = sin(x), c = cos(x);
double t = s/c;
In one case, you use a variable initialised to a value. In the other case, you use a value directly. The type of the value doesn't really matter. It could be a double, or a pointer, or a pointer to a pointer.
Why is it possible to give the address to a pointer when the function's argument is a double pointer?
These two things you mention, an address to a pointer and a double pointer, are one and the same thing. They are not two very similar things, or virtually indistinguishable, or any weak formulation like that. No, the two wordings mean exactly the same, to all digits after the decimal point.
The address of a pointer (like e.g. &pu8P2) is a pointer to a pointer.
The result of &pu8P2 is a pointer to the variable pu8P2.
And since pu8P2 is of the type uint8_t * then a pointer to such a type must be uint8_t **.
Regarding ppu8Pos, it doesn't need to be initialized, because that happens in the fnFindValue_vo function with the assignment *vF_ppu8Match = &vF_pu8Msg[i].
But there is a trap here: If the condition vF_u8Value == vF_pu8Msg[i] is never true then the assignment never happens and ppu8Pos will remain uninitialized. So that initialization of ppu8Pos is really needed after all.
The "practicality" of each solution is more an issue of personal opinion I believe, so I leave that unanswered.
For starters the function fnFindValue_vo can be a reason of undefined behavior because it does not set the pointer *vF_ppu8Match in case when the target value is not found in the array.
Also it is very strange that the size of the array is specified by an object of the type uint8_t. This does not make a sense.
The function should be declared at least the following way
void fnFindValue_vo( const uint8_t *vF_pu8Msg, size_t vF_u8Length, uint8_t **vF_ppu8Match, uint8_t vF_u8Value )
{
const uint8_t *p = vF_pu8Msg;
while ( p != vF_pu8Msg + vF_u8Length && *p != vF_u8Value ) ++p;
*vF_ppu8Match = ( uint8_t * )p;
}
The difference between the two approaches used in your question is that in the first code snippet if the target element will not be found then the pointer will still point to the first element of the array
uint8_t *pu8P2 = &u8Array[0];
And this expression
uint8_t u8Diff1 = *ppu8P2 - pu8Reference;
will yield some confusing positive value (due to the type uint8_t) because the difference *ppu8P2 - pu8Reference be negative.
In the second code snippet in this case you will get undefined behavior due to this statement
uint8_t u8Diff2 = ppu8Pos - pu8Reference;
because the pointer ppu8Pos was not initialized.
Honestly, not trying to understand your code completely, but my advice is do not overcomplicate it.
I would start with one fact which helped me untangle:
if you have int a[10]; then a is a pointer, in fact
int x = a[2] is exactly the same like int x = *(a+2) - you can try it.
So let's have
int a[10]; //this is an array
//a is a pointer to the begging of the array
a[2] is an int type and it is the third value in that array stored at memory location a plus size of two ints;
&a[2] is a pointer to that third value
*(a) is the first value in the array a
*(a+1) is the same as a[1] and it is the second int value in array a
and finally
**a is the same as *(*a) which means: *a is take the first int value in the array a (the same as above) and the second asterisk means "and take that int and pretend it is a pointer and take the value from the that location" - which is most likely a garbage.
https://stackoverflow.com/questions/42118190/dereferencing-a-double-pointer
Only when you have a[5][5]; then a[0] would be still a pointer to the first row and a[1] would be a pointer to the second row and **(a) would then be the same as a[0][0].
https://beginnersbook.com/2014/01/2d-arrays-in-c-example/
Drawing it on paper as suggested in comments helps, but what helped me a lot is to learn using debugger and break points. Put a breakpoint at the first line and then go trough the program step by step. In the "watches" put all variants like
pu8P2,&pu8P2,*pu8P2,**pu8P2 and see what is going on.
As part of our training in the Academy of Programming Languages, we also learned C. During the test, we encountered the question of what the program output would be:
#include <stdio.h>
#include <string.h>
int main(){
char str[] = "hmmmm..";
const char * const ptr1[] = {"to be","or not to be","that is the question"};
char *ptr2 = "that is the qusetion";
(&ptr2)[3] = str;
strcpy(str,"(Hamlet)");
for (int i = 0; i < sizeof(ptr1)/sizeof(*ptr1); ++i){
printf("%s ", ptr1[i]);
}
printf("\n");
return 0;
}
Later, after examining the answers, it became clear that the cell (& ptr2)[3] was identical to the memory cell in &ptr1[2], so the output of the program is: to be or not to be (Hamlet)
My question is, is it possible to know, only by written code in the notebook, without checking any compiler, that a certain pointer (or all variables in general) follow or precede other variables in memory?
Note, I do not mean array variables, so all the elements in the array must be in sequence.
In this statement:
(&ptr2)[3] = str;
ptr2 was defined with char *ptr2 inside main. With this definition, the compiler is responsible for providing storage for ptr2. The compiler is allowed to use whatever storage it wants for this—it could be before ptr1, it could be after ptr1, it could be close, it could be far away.
Then &ptr2 takes the address of ptr2. This is allowed, but we do not know where that address will be in relation to ptr1 or anything else, because the compiler is allowed to use whatever storage it wants.
Since ptr2 is a char *, &ptr2 is a pointer to char *, also known as char **.
Then (&ptr2)[3] attempts to refer to element 3 of an array of char * that is at &ptr2. But there is no array there in C’s model of computation. There is just one char * there. When you try to refer to element of 3 of an array when there is no element 3 of an array, the behavior is not defined by the C standard.
Thus, this code is a bad example. It appears the test author misunderstood C, and this code does not illustrate what was intended.
char *ptr2 = some initializer;
(&ptr2)[3] = str;
When you evaluate &ptr2, you obtain the address of memory where is stored the pointer that points to that initializer.
When you do (&ptr2)[3]=something you try to write 3*sizeof(void*) locations further from the location of ptr2, the address of a string. This is invalid and almost sure it finishes with segmentation fault.
No, it's not possible and no such assumptions can be made.
By writing outside a variable's space, this code invokes undefined behavior, it's basically "illegal" and anything can happen when you run it. The C language specification says nothing about variables being allocated on a stack in some particular order that you can exploit, it does however say that accessing random memory is undefined behavior.
Basically this code is pretty horrible and should never be used, even less so in a teaching environment. It makes me sad, how people mis-understand C and still teach it to others. :/
A program usually is loaded in memory with this structure:
Stack, Mmap'ed files, Heap, BSS (uninitialized static variables), Data segment (Initialized static variables) and Text (Compiled code)
You can learn more here:
https://manybutfinite.com/post/anatomy-of-a-program-in-memory/
Depending on how you declare the variable it will go to one of the places said before.
The compiler will arrange the BSS and Data segment variables as he wishes on compilation time so usually no chance. Neither heap vars (the OS will get the memory block that fits better the space allocated)
In the stack (which is a LIFO structure) the variables are put one over eachother so if you have:
int a = 5;
int b = 10;
You can say that a and b will be placed one following the other. So, in this case you can tell.
There is another exception and that is if the variable is an structure or an array, they are always placed like i said before, each one following the last.
In your code ptr1 is an array of arrays of chars so it will follow the exception i said.
In fact, do the following exercise:
#include <stdio.h>
#include <string.h>
int main(){
const char * const ptr1[] = {"to be","or not to be","that is the question"};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < strlen(ptr1[i]); j++)
printf("%p -> %c\n", &ptr1[i][j], ptr1[i][j]);
printf("\n");
}
}
and you will see the memory address and its content!
Have a nice day.
Gcc version:
gcc 4.4.3
The code snippet:
#include <stdio.h>
struct str {
int len;
char s[0];
};
struct test {
struct str *p_str;
};
int main()
{
struct test t = { 0 };
if (t.p_str->s) // FLAG_0
printf("here!");
printf(t.p_str->s); // FLAG_1
return 0;
}
I got a error when I run the code:Segmentation fault
I use the gdb to debug. I found it crashed at the FLAG_1
I am confused about it.
It ran OK at FLAG_0, but crashed at FLAG_1. Why ?
Meanwhile, I found the value of t.p_str is 0x00. I don't understand the if case is OK.
Note: The code is just for the study!
Since s is an array, not a pointer, it will never be null. So the compiler is free to omit the check, and assume it isn't null. If it does that, then FLAG_0 will not attempt to dereference the null pointer, so you won't get a segmentation fault at that point.
Of course, it's free to do anything else it feels like, since the program has undefined behaviour.
If you're talking about C++ it's illegal to declare an array of length 0. C++11 §8.3.4 Arrays:
If the constant-expression is present, it shall be an integral
constant expression and its value shall be greater than zero.
The constant-expression here is the array length. However, the standard allows creating zero length arrays dynamically using new.
If you're talking about C, then it's already entered undefined behaviour territory at FLAG_0 and thus nothing that happens afterwards is warranted by the language.
You declared an Array with 0 elements. char s[0];.
An empty string has at least one element, a binary zero.
This struct test t = { 0 }; does not initialise your structure to 0. It initialises your first element to 0. In this special case you just create a null pointer.
You can initialise using memset.
#include <string.h>
typedef struct{
int len;
char s[10];
}MyStr;
int main()
{
MyStr str;
memset(&str, 0, sizeof(MyStr));
}
One statement dereferences a nullpointer: printf(t.p_str->s); tries to resolve t.p_str->s with t.p_str beeing 0.
So first of all, the code is total rubbish and invokes undefined behaviour in multiple places, so anything can happen, including crashes or non crashes, unexpected or not.
What is t.p_str->s ?
s is an array. If t.p_str were a valid pointer, then t.p_str->s would be the address of the first character in the s array. That's a pointer, it's not a null pointer, so for the "if" statement that counts as a true result. The compiler doesn't actually need to evaluate the whole expression, because it doesn't care what pointer it is, only that it isn't a null pointer. That's why t.p_str->s doesn't crash here, because the program never evaluates it.
In the printf statement, the actual value of t.p_str->s is needed, so that one crashes.
Hi I am trying to make a program with pointer in a struct. Compiler appears no problems but the program crashes.
Could you help me please ?
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int pos;
typedef struct _Parking Parking;
struct _Parking
{
int pos;
char name[15];
char description[80];
float price;
int slots[5];
char last_driver_id;
int reservations;
};
Parking *myaccounts;
int create_Parking()
{
strcpy(myaccounts->name,"Pro");
myaccounts->pos ++;
return pos-1;
}
int main()
{
int a;
a = create_Parking();
printf("a=%d\n",a);
printf("name=%s\n",myaccounts->name);
system("pause");
return 0;
}
Your myaccounts pointer is initialized to NULL (as a global variable) and does therefore not point to usable memory. Try the manual page for malloc for more information.
Edit: Incorporated Maciej's comment.
You never allocate any memory for "myaccounts".
Pointers in C do not point to valid memory (and will crash if you try to use them), until you specifically point them somewhere valid by using the address-of operator on an object (&) or by allocating memory for them and assigning that address into the pointer (malloc() and friends). Of course if you use the address-of operator that location can go invalid when the object goes out of scope. If you use malloc() that location can go invalid when free() is called. In either case, your pointer will become invalid again.
C is hugely reliant on pointers too, so you can count on any C code you write of any size having a bug or two of this nature until you track them down and fix them. Getting your sources past the compiler in C really doesn't mean much. If you want to write in a language where your code is liable to work first time you run it after getting it past the compiler, you want Ada.
Your pointer doesn't point to anything. You can try either one of these:
Parking myaccountsInstance;
Parking *myaccounts = &myaccountsInstance;
Or in the main function:
Start with:
myaccounts = (Parking*)malloc(sizeof(Parking));
And end with:
free(myaccounts);