English 中文(简体)
Passing a pointer to a char array to a function
原标题:

I have a pointer to my char array like this.

    unsigned char *recvBuf;
    recvBuf = (unsigned char *)malloc(sizeof(char)*RECVBUF);

I then pass that to a function defined as

void setHeader(MyHeader *myHeader, unsigned char *buffer)

Which copies the first 12 bytes into a struct

Then I try and pass it to another function later on

Record readRecord(unsigned char *buffer, int *arrayPosition, int buffsize)

But the program always becomes unresponsive when I try and access any part of the array using [], in the exact same way it was used in the previous functiom. And i m also fine to access it like that before i pass it to the function.

Ideas would be much appreciated.

Thanks

EDIT:

Pointers are the same.

This is inside the second function

I added this code to the top and it works just fine

int k = *arrayPosition;
    printf("%p 
", buffer);

    printf("%c 
", buffer[k]);

This is the original code, the program becomes unresponsive adfter i = *arrayposition...

int i, j;
Record *myRecord;
Resource *myResource;
unsigned char name[255];
unsigned char data[50];
unsigned int offset;

i = *arrayPosition;

while(buffer[i] !=   )
{
    if((buffer[i] & 0xC000) == 0xC000)
    {
问题回答

Really need more context to assist here, there s nothing obviously untoward from what you ve said.

Basics:

  1. Check that you haven t indexed off the end of your memory block.
  2. Check that you re not passing &recvBuf where you mean to pass recvBuf? A compiler should catch this, but you d trash your stack if you did this, maybe using a cast.

Input is not enough to be sure of the problem that makes the pointer go bad but a good catch would be to check with gdb if the pointer changes between the function call you descripted.

Do you modify any char of the array? You should dereference the pointer with * in that case.

If you cannot use the pointer after the call causes can be:

  1. it is changed erroneously inside the function
  2. it is a pointer to an allocated value in the stack that is deallocated implicitly somewhere (but it sounds strange)




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签