Setting The Members of a C Struct [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
some_struct = some_function; //some_function returns a pointer to an instance of a struct
some_struct->num = 8; //num is an int
The second line creates a segmentation fault, when I try to use gdb with the p some_struct->num command, it says Cannot access memory at address 0x0
How do I set the value of some_struct->num without creating a segmentation fault?

Your function is returning a NULL pointer. Make sure it returns valid memory for a some_struct type.

Related

Argument value in C immutable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have the following code:
void func(uint8 *var) {
uint8 tempvar;
if (var)
var = &tempvar;
*var = 0;
}
I call the function using:
func(NULL);
The code gives a segmentation fault at the line "*var = 0;" because var still points to the memory address 0x0. I don't understand why my assignment to a temporary variable did not work!
Because you omitted the !. You are testing whether the variable exists, but you should test whether it doesn't exist: if (!var) ...
To expand a bit further... var is of type uint8 *, thus var itself is a pointer. By writing if (var) you are testing if that pointer is not NULL. In pseudo-code, your code says:
if (the var pointer already exists)
assign a new pointer to it (make it point to somewhere else)
But if it doesn't exist (if it points to NULL), you leave it alone. You can verify this using a debugger or a simple print statement. Thus you end up at the assignment with a null-pointer, and crash your program.

How to allocate an array with NULL [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Can someone tell me how should I do to allocate every v[i].word and next with NULL?
Struct hash{
Char*word;
Hash*next;
}*v[10];
Make a for loop, let it iterate from i = 0 to 9. In each iteration, allocate one struct hash and point v[i] to it. Afterwards, initialize *v[i] as needed. Don't forget to check if malloc() failed.

Usage of pointer in C programming [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am encountering a difficulty in trying to understand the usage of pointers in C programming. I do not understand why this doesn't compile:
void func(char**p);
void other_fun(void)
{
char arr[5][3];
func(arr);
}
The major issue is that arrays are not the same as pointers. Syntactically, you can use them in a very similar manner within a function, but they are not the same. As such, what you are passing in func(arr) is a variable of type char (*)[3], or a pointer to an array of 3 chars, and not a pointer to a pointer, which is what func expects.

How can an argument be different in the caller and the callee? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm debugging a program and ran across something I've never seen before. Below is an excerpt from gdb.
1236 size = init_text_buffer(fn);
(gdb) p fn
$13 = 0x7fff1cd22d80 "-"
(gdb) s
init_text_buffer (fn=0xd00 <error: Cannot access memory at address 0xd00>)
at editors/vi.c:720
720 {
The function init_text_buffer is called with a char pointer with the value 0x7fff78136bd0. As I step into the function the argument suddently has a different value.
What are possible causes of this? I'm not asking you to debug my code (I didn't include any so how could you?), I just need a pointer in the right direction. This thing has left me with no clues as to what to look for at all.
You should go into hybrid assembly mode (Ctrl+x 2) and do stepi to examine which instructions are actually being performed. I had this recently - in my case it was an optimization that the C code of course didn't reveal. In your case, it could reveal a memory overrun.
Worth a shot.

fopen() leading to a segmentation fault [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have the file name of a file stored in char *names. When I use fopen(&names[0],"r"), I am receiving a seg fault error. Why is this happening and how can I fix it?
You already have a pointer to char as "names", so you could simplify that.
You can just pass that into fopen().
So as follows:
char * names = "/home/user/test.txt";
FILE * file = fopen(names, "r");
For the seg fault, we'd probably need to see what's in "names" or what it points to.

Resources