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

#include <stdio.h>
#include <conio.h>

void main()
	char a[4],*b;

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..

11 Years
Discussion Span
Last Post by Narue


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.


>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:

*( 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.

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.

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:

#include <stdio.h>

int main ( void )
  char a[] = "test";
  char *b = a;

  *( b + 2 ) = '!';
  printf ( "%s\n", a );
  printf ( "%s\n", b );

  return 0;

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.


>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.


could u pls tell how to do the conversion from an object to a pointer to a char..


>could u pls tell how to do the conversion from an object to a pointer to a char..

#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;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.