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.
Related
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.
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In eclipse it don't recognize the type size_t, it write error on it and a message uknown type name 'size_t',
it is written right
size_t comes from stddef.h standard header file. You have to include it. You can also get it from stdio.h where it is also declared.
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 just started getting a weird printf output, has anyone ever seen this? Any idea what it could be caused by?
http://imgur.com/4Mt6xdi
Edit
Here's the code. I'm new to c so if anything (even if it's not causing the error) looks wrong or uncommon please tell me.
printf(f2,"%8.31f\t%8.31f\n",x[0],x[2]);
fprintf(f2,"%8.31f\t%8.31f\n",x[1],x[2]);
In the code you write:
if (x[0]*oldx<0)
{
printf(f2,"%8.31f\t%8.31f\n",x[0],x[2]);
fprintf(f2,"%8.31f\t%8.31f\n",x[1],x[2]);
}
where f2 is a pointer to FILE, which shall not be passed as the first parameter of printf. Just remove it.
At least one problem is on lines 96-97:
printf(f2,"%8.31f\t%8.31f\n",x[0],x[2]);
fprintf(f2,"%8.31f\t%8.31f\n",x[1],x[2]);
The first line should call fprintf, not printf.
Any compiler should give you at least a warning for calling printf with a FILE* as the first argument. Did you see such a warning? If so, why did you ignore it?
Compiling with additional warnings enabled should show you a number of other problems. Fix those before doing anything else.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to write some data into a file. But I have too many data and each one should be named through a certain structure depending on the number of nodes and number of edges each data file has.
For example: for a data having 10 nodes and 20 edges, its name will be s_10_20.txt
for a data having 14 nodes and 30 edges, its name will be s_14_30.txt
the structure I'm following is: s_numOfNodes_numOfEdges.txt
(knowing that numOfNodes and numOfEdges are already scanned from the data file.)
The code I wrote is:
FILE *fp;
fp=fopen("s_%d_%d.txt",numOfNodes,numOfEdges,"w+");
This is giving me the following errors:
passing argument 2 of fopen makes pointer from integer without a cast
too many arguments to function fopen
How can I write it in any other way?
sprintf function can help you.
char file_name[MAX_PATH];
sprintf(file_name, s_%d_%d.txt",numOfNodes, numOfEdges);
fopen(file_name, "w+");