| | |
Pointer Problem
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 29
Reputation:
Solved Threads: 3
here is the code....
it compile perfectly. but on executing it is giving segmentation fault.
where is the problem?
C Syntax (Toggle Plain Text)
#include<stdio.h> int main() { int *k; *k=10; printf("%d %d",k,*k); return 0; }
it compile perfectly. but on executing it is giving segmentation fault.
where is the problem?
BOOM! That's a technical term!
Your pointer needs to be pointing at real data!
Your pointer needs to be pointing at real data!
C Syntax (Toggle Plain Text)
int *k, v; k = &v; // k now points to the address of {v}. *k=10; // So we are now storing the value // reference by k, meaning v is now equal to 10.
Last edited by wildgoose; Jul 6th, 2009 at 11:34 am. Reason: typo
•
•
Join Date: Apr 2009
Posts: 29
Reputation:
Solved Threads: 3
Can u plz elaborate what dose a real data mean...
After adding malloc ,this code compile and execute perfectly ...
can u explain it?
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<stdlib.h> int main() { int *k; k=(int*)malloc(sizeof(int)); *k=10; printf("%d %d",k,*k); return 0; }
After adding malloc ,this code compile and execute perfectly ...
can u explain it?
•
•
Join Date: Apr 2009
Posts: 7
Reputation:
Solved Threads: 2
you can't use *k=10, because pointer points to place in memory...10 doesn't have place in memory its just a number.you need to have variable on which pointer is going to point (which has place in memory ). for example :
also incorrect :
j=&(k+5)
j=&(a==b)
&a=&b
&a=150 (j , a, b random variables)
if you don't understand why just ask..
c Syntax (Toggle Plain Text)
int *k; int a=10; k=&a // &a is address of variable a printf("%d", *k); //*k is pointing which address to print printf("%d", k);//prints address of variable a
also incorrect :
j=&(k+5)
j=&(a==b)
&a=&b
&a=150 (j , a, b random variables)
if you don't understand why just ask..
Last edited by trshaaa; Jul 6th, 2009 at 11:59 am.
Real Data? You are pointing nowhere! Whatever value was in your pointer at init time is where it will try to point! But there is no assigned memory there to store that value!
Why are you using malloc() for a simple integer? But if you do use malloc() don't forget your release!
You're now trying to print the address and the value in signed decimal? Printing the address isn't going to make any sense! But if you do, you may want to print it in hex.
Why are you using malloc() for a simple integer? But if you do use malloc() don't forget your release!
You're now trying to print the address and the value in signed decimal? Printing the address isn't going to make any sense! But if you do, you may want to print it in hex.
•
•
•
•
After adding malloc ,this code compile and execute perfectly ... can u explain it?
when you first declare this pointer, there is no memory allocated for it and the address that it points to is undefined.
malloc() then causes a location of memory to be allocated (in your example for a single integer) and the pointer 'k' is assigned to the address of this allocated memory space.
the *value* contained at this memory space is still undefined, until the line of code
*k = 10 , which assigns the value 10 to the memory pointed to by 'k'as wildgoose suggested, to use malloc for a single integer is not very practical. malloc is for dynamically sizing arrays, not single variables. you should not get in the habit of using it like this. You should instead do it the way he showed you in Post #2
in any event, it will make more sense to print the address as a hex value. use the format specifier '%p' to print a pointer as a hex address:
printf("address %p = %d\n", k, *k); see http://www.faqs.org/docs/learnc/c620.html
.
Last edited by jephthah; Jul 6th, 2009 at 12:56 pm.
•
•
Join Date: Apr 2009
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
Can u plz elaborate what dose a real data mean...
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<stdlib.h> int main() { int *k; k=(int*)malloc(sizeof(int)); *k=10; printf("%d %d",k,*k); return 0; }
After adding malloc ,this code compile and execute perfectly ...
can u explain it?
for example,
C Syntax (Toggle Plain Text)
int *k; k=(int *)malloc(6*sizeof(int)); *k=10; free(k);
•
•
•
•
when we do free the memory space reserved by malloc..does it free only the memory space not being used at that time or the whole memory space allocated by malloc???
"You can't build a reputation on what you are going to do."
![]() |
Similar Threads
- Pointer Problem. (C++)
- pointer problem (C++)
- C++ Array pointer problem (C++)
- linked list pointer problem (C++)
- Dynamic Array of Structures, Loop problem! (C++)
- Help with code (C)
- C pointer problem (C)
- pointer problem (C++)
Other Threads in the C Forum
- Previous Thread: IIC Interrupt Handler
- Next Thread: Re: mp3 compression
Views: 400 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C
api array arrays bash behaviour binary binarysearch c++ calculate calculator char code coke command conversion convert copyimagefile cprogramme database decimal directory dude dynamic error exec fflush(stdout) file fork function functions getlogicaldrivestrin givemetehcodez grade graphics hacking homework i/o ide inches input int integer km lazy library line linked linkedlist linux list locate loop malloc matrix memory mysql no-code no-effort number operator output path pause performance pointer pointers problem probleminc process program programming read recursion recursive recv refresh reverse roman scanf segmentationfault sms_speak socketprograming spoonfeeding strchr string strings strtok structures student syntax system systemcall test turbo-c turboc unix user variable wab windows






