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 1 year ago.
Improve this question
Why this version of C code is not working (cause segmentation fault)
#include <stdio.h>
int main()
{
int *p;
*p = 10;
return 0;
}
while this one is working?
int main()
{
char c = 'c';
int *p;
*p = 10;
return 0;
}
Both code snippets are wrong, we can't say that one is more wrong than the other, p is uninitialized in both cases and therefore it may or may not contain a valid memory address, it's impossible to predict, this means that the behavior is undefined. That being the case, working, whatever that may mean, is well within the realm of possible outcomes.
For int* p, no storage space is allocated for the actual integer number.
You need to modify your code as follows to make it work properly.
In C++, you can use new, but in C you can use malloc.
#include <stdio.h>
int main()
{
int* p = malloc(sizeof(int));
*p = 10;
free(p);
return 0;
}
Related
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 4 years ago.
Improve this question
I dunno why this doesn't work. the code has a problem with the *c in
charToInt function but should be a legal statement in c. at least so I thought. I am excited to learn something new here.
int charToint(char *c) {
return *c - '0';
}
int main(void) {
char c = '3';
printf("%d\n", charToint(c));
{
You're passing a char to a function that expects a char *. Your compiler should have warned you about this. The value of that char is then interpreted as a pointer value and dereferenced. Dereferencing an invalid pointer invokes undefined behavior, which in this case results in the program crashing.
The function is ultimately trying to work with a char, so change it to accept a char:
int charToint(char c) {
return c - '0';
}
Alternately, you can leave the function as it and pass it a pointer:
printf("%d\n", charToint(&c));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
when you are declaring static variables in C,
say you wrote a program as such:
int *c;
void foo(){
c = (int *) malloc (10*sizeof(int));
c = &c[3];
*c = *&c[3];
}
What does it mean to have *c? I mean I understand that * means that it points to something but what does *c do?
and also, why do you need to cast (int *) the return value of malloc()?
when you are declaring static variables in C
Not related to this question, or atleast to the code you've shown.
but what does *c do?
Assuming your question related to the statement *c = *&c[3];, it refers to the object at address held by c.
why did you have to cast (int *) in front of malloc?
You should not. Please do not cast the return value of malloc() [and family].
Note: You code snippet is very poor and bad practice, most likely to be invalid. c = &c[3]; obvious memory leak.
I mean I understand that * means that it points to something but what does *c do?
The * does not always mean that it point to something.
Example:
If you write this:
int* c;
It means c is a pointer points to an int variable.
When you do like this:
int* c = &x;
*c = 5;
The second * is pointer c dereference.
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 9 years ago.
Improve this question
Got xCode 5.0.2 bought mac yesterday and do not understand why this simple code doesn't work.
#include "stdio.h"
int main(){
int N;
printf("vvedite koli4estvo dannih\n");//mistake and warning is here
scanf("%d", &N);
int *arr = new (int [N]);
return 0;
}
mistake is
expected expression
implicit declaration of function 'new' is invalid in c99
Your code is written in C but you are using new; a C++ operator. Use malloc instead.
int *arr = malloc(sizeof(int)*N); // allocates memory for N itegers
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 8 years ago.
Improve this question
it seems that this is a duplicate question, but I searched stackoverflow's question about that point and non is like to my problem(I think)
I've two variables of a struct each has its own pointer to char, when I tried to copy from one variable's string to another variable's string, nothing happened, although no errors appear, just warning
implicit declaration of function strcpy
incompatible implicit declaration of built-in function 'strcpy'
I read from some questions on stackoverflow that you'd better to use strdup() function instead of strcpy() but when I did, I had an error
too many arguments to function 'strdup'
I read that there's a problem with strcpy() called "segmentation fault" and I knew it's about memory allocation, I don't totally understand what it's exactly and don't know if it's the problem with my code?
and this is my code
struct p
{
char *name;
struct p *next;
};
struct p *ptrary[10];
int main(void)
{
struct p p,q;
p.name="xyz";
p.next=NULL;
ptrary[0]=&p;
strdup(q.name,p.name);
ptrary[1]=&q;
printf("%s\n",ptrary[1]->name);
return 0;
}
so what is the problem and how I can solve it?
strdup() takes only one argument; it malloc's and returns a new block of heap memory containing the duplicated string. (See https://stackoverflow.com/questions/252782/strdup-what-does-it-do-in-c)
Which probably also points to the problem you were having before -- were you remembering to malloc the space for q to copy p's contents into?
change
strdup(q.name,p.name);
to
q.name = strdup(p.name);
see man page of strdup for further details. The strdup() function returns a pointer to a new string.
full code:
#include <stdio.h>
#include <string.h>
struct p
{
char *name;
struct p *next;
};
struct p *ptrary[10];
int main(void)
{
struct p p,q;
p.name="xyz";
p.next=NULL;
ptrary[0]=&p;
q.name = strdup(p.name);
ptrary[1]=&q;
printf("%s\n",ptrary[1]->name);
return 0;
}
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
# include <stdio.h>
# include <stdlib.h>
int main(int argc, char *argv[])
{
int a[5][10][2];
int *p;
p = (int(*)[10][2])p;//Gives error!
return EXIT_SUCCESS;
}
I want to type cast p to type so that it can act as a pointer to the given 3-d array?Is there a way to do so.I am applyindg the idea that the type of a variable is everything except variable name.
Why are you trying to "typecast" anything? Why would you expect a value "typecasted" to (int(*)[10][2]) to be compatible with an int * pointer? And why does your original code assigns p to p, completely ignoring a?
This is what you can do
int a[5][10][2];
int (*p)[10][2] = a;
Now p is a pointer that can be used to access a, i.e. p[i][j][k] is equivalent to a[i][j][k]. No typecasting necessary.
If you write p = (int(*)[10][2])a; it won't give you any errors, may be a warning. You are thinking that p will be converted to pointer to a 3-D array, which is wrong. Try this statement after assigning a to p.
printf("addresses %u %u",p,p+1);
According to you, output should be something similar to this(lets say) "addresses 9990000 99940000", because you are thinking p is pointing to 3-D array. However you will get similar to "addresses 9990000 9990004", proving that p is a pointer to an integer.