help needed in assigning values..

Reply

Join Date: Jun 2005
Posts: 33
Reputation: shre86 is an unknown quantity at this point 
Solved Threads: 1
shre86 shre86 is offline Offline
Light Poster

help needed in assigning values..

 
0
  #1
Jul 3rd, 2005
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

  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. void main()
  5. {
  6. char a[4],*b;
  7. a[0]=2;
  8. a[1]=2;
  9. b[0]=a[0];
  10. printf("%s",a);
  11. getch();
  12. }
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..
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: help needed in assigning values..

 
0
  #2
Jul 3rd, 2005
Hi,

You have made a declaration of your pointer *b, but where is the definition of it

A pointer is an adress and must be assigned towards one, meaning, you'll need to use 'new' to assign an adress towards b.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 722
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: help needed in assigning values..

 
0
  #3
Jul 3rd, 2005
>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:
  1. array[index]
Is converted by the compiler internally to this:
  1. *( array + index )
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:
  1. #include <stdio.h>
  2.  
  3. int main ( void )
  4. {
  5. char a[] = "test";
  6. char *b = a;
  7.  
  8. *( b + 2 ) = '!';
  9. printf ( "%s\n", a );
  10. printf ( "%s\n", b );
  11.  
  12. return 0;
  13. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 33
Reputation: shre86 is an unknown quantity at this point 
Solved Threads: 1
shre86 shre86 is offline Offline
Light Poster

Re: help needed in assigning values..

 
0
  #4
Jul 3rd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 722
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: help needed in assigning values..

 
0
  #5
Jul 3rd, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 33
Reputation: shre86 is an unknown quantity at this point 
Solved Threads: 1
shre86 shre86 is offline Offline
Light Poster

Re: help needed in assigning values..

 
0
  #6
Jul 4th, 2005
could u pls tell how to do the conversion from an object to a pointer to a char..
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 722
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: help needed in assigning values..

 
0
  #7
Jul 4th, 2005
>could u pls tell how to do the conversion from an object to a pointer to a char..
  1. #include <stdio.h>
  2.  
  3. int main ( void )
  4. {
  5. int x = 12345;
  6. char *p = (char *)&x; /* Convert to pointer to char */
  7. int *q = (int *)p; /* Restore to int representation */
  8.  
  9. printf ( "%d\n", *q );
  10.  
  11. return 0;
  12. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC