| | |
help needed in assigning values..
![]() |
•
•
Join Date: Jun 2005
Posts: 33
Reputation:
Solved Threads: 1
i am writing a program.. in that i am needed to represent an array using pointers only..(its a constraint i ve to work with).. the prob i am facing can be shown by this small program.. pls help me rectify with
i have trouble whenever i try to assign b a value.. like here b[0]=a[0] gives the exception.. even if i have b[0]=0 it will give an exception
the program is giving an nullreferenceexception.. saying that i cannot dereference a null object.. how do i fix this.. or are there any alternate ways to handle this..
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <conio.h> void main() { char a[4],*b; a[0]=2; a[1]=2; b[0]=a[0]; printf("%s",a); getch(); }
the program is giving an nullreferenceexception.. saying that i cannot dereference a null object.. how do i fix this.. or are there any alternate ways to handle this..
>i am needed to represent an array using pointers only
That suggests that you're required to use pointer arithmetic and dereferencing rather than subscripting. But, because the requirements clearly say "array", and not "dynamic array", or "simulated array" (granted, most teachers aren't intelligent enough to make the distinction), I would wager that you can still use an array and call it good.
To replace subscripting with pointer arithmetic, you need only realize that this:
Is converted by the compiler internally to this:
Now, let's shred your code. 
>#include <conio.h>
This is a pointless include. You use getch, but only to "keep the window open" when running your program. That basically restricts the number of compilers you can run this code in to the ones that support getch in conio.h. Now, you could use getchar from stdio.h and have your code run everywhere. Using getch for this purpose is a naive rookie mistake that you would do well to rid yourself of as soon as possible.
>void main()
main returns int, there is no wiggle room here. If you want your code to be correct for all hosted implementations, main returns int. And when you say main returns int, you need to actually return a value too. 0 is a good choice for successful termination.
>b[0]=a[0];
Okay, and what does b point to again? If you said "I dunno", you get a cookie, because that's precisely where it points! Before using a pointer, you make sure it points to memory that you own, otherwise your program is probably undefined and seriously broken.
>printf("%s",a);
What makes you think that a contains a remotely reasonable string? a[0] is 2, a[1] is 2. Not only is that likely to have interesting output, you also failed to set a[3] to '\0' so that printf doesn't walk all through memory you don't own looking for a null character. Until you know how strings work, you would be better off initializing a string when you declare it:
That suggests that you're required to use pointer arithmetic and dereferencing rather than subscripting. But, because the requirements clearly say "array", and not "dynamic array", or "simulated array" (granted, most teachers aren't intelligent enough to make the distinction), I would wager that you can still use an array and call it good.
To replace subscripting with pointer arithmetic, you need only realize that this:
C Syntax (Toggle Plain Text)
array[index]
C Syntax (Toggle Plain Text)
*( array + index )

>#include <conio.h>
This is a pointless include. You use getch, but only to "keep the window open" when running your program. That basically restricts the number of compilers you can run this code in to the ones that support getch in conio.h. Now, you could use getchar from stdio.h and have your code run everywhere. Using getch for this purpose is a naive rookie mistake that you would do well to rid yourself of as soon as possible.
>void main()
main returns int, there is no wiggle room here. If you want your code to be correct for all hosted implementations, main returns int. And when you say main returns int, you need to actually return a value too. 0 is a good choice for successful termination.
>b[0]=a[0];
Okay, and what does b point to again? If you said "I dunno", you get a cookie, because that's precisely where it points! Before using a pointer, you make sure it points to memory that you own, otherwise your program is probably undefined and seriously broken.
>printf("%s",a);
What makes you think that a contains a remotely reasonable string? a[0] is 2, a[1] is 2. Not only is that likely to have interesting output, you also failed to set a[3] to '\0' so that printf doesn't walk all through memory you don't own looking for a null character. Until you know how strings work, you would be better off initializing a string when you declare it:
C Syntax (Toggle Plain Text)
#include <stdio.h> int main ( void ) { char a[] = "test"; char *b = a; *( b + 2 ) = '!'; printf ( "%s\n", a ); printf ( "%s\n", b ); return 0; }
I'm here to prove you wrong.
•
•
Join Date: Jun 2005
Posts: 33
Reputation:
Solved Threads: 1
ok.. i realise the example i gave was very wrong and not related to what i want..
my prob is as follows..
i am writing a network program.. after sending using WSASend i am receiving "buffer" of type WSABUF..
now i am not able to access the value at each index of what i am receiving..
as i am sending numbers which have to be converted to strings first i need to access each position in receiving buf to proceed further.. (i.e recalculate the number using some technique i am using)
pls give me a way of how to access value at every index in buffer.buf??
otherwise pls gimme a way of sending using windows sockets functions any data type as only string is allowedto be sent usind send and WSASend fn.
my prob is as follows..
i am writing a network program.. after sending using WSASend i am receiving "buffer" of type WSABUF..
now i am not able to access the value at each index of what i am receiving..
as i am sending numbers which have to be converted to strings first i need to access each position in receiving buf to proceed further.. (i.e recalculate the number using some technique i am using)
pls give me a way of how to access value at every index in buffer.buf??
otherwise pls gimme a way of sending using windows sockets functions any data type as only string is allowedto be sent usind send and WSASend fn.
>pls give me a way of how to access value at every index in buffer.buf??
So you converted integer values to strings and now you want to get the integers back? Yep, that's completely different from your original question. :rolleyes:
>otherwise pls gimme a way of sending using windows sockets functions any data type
You can convert any object into a pointer to char without losing information. So there's really no need to convert your numbers into strings to pass them through a socket.
So you converted integer values to strings and now you want to get the integers back? Yep, that's completely different from your original question. :rolleyes:
>otherwise pls gimme a way of sending using windows sockets functions any data type
You can convert any object into a pointer to char without losing information. So there's really no need to convert your numbers into strings to pass them through a socket.
I'm here to prove you wrong.
>could u pls tell how to do the conversion from an object to a pointer to a char..
C Syntax (Toggle Plain Text)
#include <stdio.h> int main ( void ) { int x = 12345; char *p = (char *)&x; /* Convert to pointer to char */ int *q = (int *)p; /* Restore to int representation */ printf ( "%d\n", *q ); return 0; }
I'm here to prove you wrong.
![]() |
Similar Threads
- Assigning values to variables (ASP)
- assigning arrays in VB (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: Need help for simple program, don't have a clue about C
- Next Thread: problem in generating non repeated random numbers
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi







