CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

problem with pointer to an array
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

problem with pointer to an array
PostPosted: Fri Oct 20, 2006 8:15 am     Reply with quote

hi people,

does not ccs suport it ?

im trying to pass buffer's addres to a routine... but i gave some errors.

char Buffer[10];

routine (char **Buffer); --> routine (&Buffer);


doesnt work.


regards.
_________________
Andre
Neckruin



Joined: 17 Jan 2006
Posts: 66

View user's profile Send private message

PostPosted: Fri Oct 20, 2006 9:16 am     Reply with quote

If you wish to pass the buffer address you have to do like this:

Code:
char Buffer[10]

void Function(char *buf) {...}

Function(Buffer);


It's like ANSI C... or very similar

Regards,

Juanma
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Fri Oct 20, 2006 9:51 am     Reply with quote

sorry.. but it isnt my purpose..


i want to change values of buf by reference..

like this:

Code:

change (&buf);

change (char ** buf)
{
   char i;

   for (i = 0; i < 8; i++)
   {
          (* buf)[i] = i;
    }

   return;
}




got it ?


regards
_________________
Andre
Neckruin



Joined: 17 Jan 2006
Posts: 66

View user's profile Send private message

PostPosted: Fri Oct 20, 2006 10:16 am     Reply with quote

If you wish to change the value of, for example, Buffer[i], I mean, the content of that "line" of the buffer, you can do it like this:

Code:

void Function(char *Buffer)
{
     int i = 0;
     for(i=0; i<LIMIT; i++)
          Buffer[i] = value;
}


Then, to call that function in order to modify tha content of your Buffer you must do like this:

Code:
char MyBuffer[SIZE];

Function(MyBuffer);


This should work, but anyway I haven't tried out it right now. I say it should work because the name of the char array is a pointer itself.

Juanma
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Fri Oct 20, 2006 10:25 am     Reply with quote

Juanma,

ill try..

but its quite diferente for the ANSI C, because you need to pass and address to change its value... and no pass the value to change it...

but ill try.


thanks
_________________
Andre
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Oct 20, 2006 10:34 am     Reply with quote

long hand works...


Code:
typedef char* pchar;

pchar *buf;

_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
jspencer



Joined: 22 Dec 2003
Posts: 57
Location: Boise, ID USA

View user's profile Send private message

PostPosted: Fri Oct 20, 2006 11:09 am     Reply with quote

Code:

int16 nal;
read_eeprom_block(&nal, 2, NEXT_ADDRESS_LOCATION);


void read_eeprom_block(int8 *mem_address, int8 num_bytes_2_read, int16 address_location) {
   int8 i;
   for (i=0;i<num_bytes_2_read;i++)
      *(mem_address + i) = read_ext_eeprom(address_location + i);
}

Is this what you were looking for? It is not an array, but a int16. Should work the same for a buffer.
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Fri Oct 20, 2006 11:34 am     Reply with quote

jspencer,

is almost it =)

but you take the point.. when you want to set a value for a variable by reference, you have to pass its address...

so if you have an int8 variable, u just pass a pointer to the function...
like you did.

but when you have a vector of int8 you have to pass a pointer to pointer.. or point to array...

insted you do function (int8 *var)
you need to do function (int8 **var)

got it ?

and ccs doesnt support that !!

thanks for your help.
_________________
Andre
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Fri Oct 20, 2006 11:35 am     Reply with quote

asmallri

hmmmm, i wont thought about your sugestion... ill gonna try.
_________________
Andre
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Fri Oct 20, 2006 11:37 am     Reply with quote

asmallri


it didnt work at all =(
_________________
Andre
JPA
Guest







PostPosted: Sun Oct 22, 2006 9:27 am     Reply with quote

I have read all the posts and I still do not understand what you want to do.

If you declare
char Buffer[10];

then: Buffer is a char * and points to the first element of the array.

Buffer[i] is completely equivalent to *(Buffer+i)

In your 1st post, as you have declared
char Buffer[10];
the expression &Buffer has no meaning.

If you want to modify the contents of Buffer in a function, you just have to call the function with Buffer as argument: the_funtion(Buffer).
Inside that function, if you write Buffer[i] = 10, you will modify the original Buffer.

If you need more explanation, I advise you to read the "Bible" of C programmers, the K&R book.

JPA
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Sun Oct 22, 2006 10:21 am     Reply with quote

Dude, you dont understand.

you can use a pointer to an array in C (im not a newbie in C)

if you want to pass the buffer itself you need a pointer to it..

as you shown above, you are passing a pointer to a position of a vector.

look at that..

i want to create a general function for all size of buffers like that:

Code:

void ZeroValue (char **Buffer, int8 Lenght)
{
    int8 i;


    for (i = 0; i < Length; i++)
    {
          (* Buffer)[i] = '0';
     }

      return;
}




and you call this function like that:

Code:

void main ()
{
    int8 i;
    char Buffer[20];

    for (i = 0; i < 20; i++) Buffer[i] = '1';   // here we fill the buffer with '1'
    for (i = 0; i < 20; i++) printf ("%c", Buffer[i]); // send the values to rs232, and you will receive all values '1'


    ZeroValue (&Buffer, 20);           // changing values

    for (i = 0; i < 20; i++) printf ("%c", Buffer[i]); // send the values to rs232, and you will receive all values '0'


   return;
}


in this way i dont need to pass just a single position of the buffer, i just pass the complete buffer.


got it ?


just try to do it in a C compiler for PC. You�ll see that works

regards.
_________________
Andre
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Sun Oct 22, 2006 10:50 am     Reply with quote

You can't pass an array on an embeded chip. You must pass a pointer to the memory location the array already exist at. When you add the length to the argument you can modify memory location from the pointer for length locations.

There are some things that can not be done with an embeded chip. The C language is supposed to adapt to the platform you compile for. For example variable types such as a long are not the same for each platform.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 22, 2006 4:57 pm     Reply with quote

I can't get your test program to compile under MSVC++ 6.0 (as a .cpp)
or under MSVC vs. 1.52 (as a .c program).

I changed all the 'int8' declarations to be 'int', and I fixed the mis-spelling
on 'Length'. I get the following output with MSVC vs. 1.52:
Quote:

Initializing...
Compiling...
c:\msvc\projects\test\test.c
c:\msvc\projects\test\test.c(35) : warning C4047: 'argument' : different levels of indirection
c:\msvc\projects\test\test.c(35) : warning C4024: 'ZeroValue' : different types for formal and actual parameter 1
TEST.C - 0 error(s), 2 warning(s)

Both of those errors refer to this line:
Code:
ZeroValue (&Buffer, 20);  // changing values

The reason that I tested it under MSVC is because it's implicit in your post
that the code will work under "real C", but fails in CCS. Can you post
a revised test program that will compile in MSVC ?
andreluizeng



Joined: 04 Apr 2006
Posts: 117
Location: Brasil

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Sun Oct 22, 2006 6:50 pm     Reply with quote

Hi people..


PCM, here is the code.. you can compile in any compiler.

Code:


#include <stdio.h>
#include <stdlib.h>

void InitBuf (char **Buf, int length, char Value)
{
        int i;

        for (i = 0; i < length; i++)
        {
                (* Buf)[i] = Value;
        }

        return;
}

int main ()
{
        char *Buf;
        int i;

        Buf = (char *) malloc (sizeof (char) * 10);

        InitBuf (&Buf, 10, 'A');

        printf ("First Values: ");
        for (i = 0; i < 10; i++)
        {
                printf ("%c ", Buf[i]);
        }


        InitBuf (&Buf, 10, 'B');

        printf ("\nSecond Values: ");
        for (i = 0; i < 10; i++)
        {
                printf ("%c ", Buf[i]);
        }

        return 0;
}



i change a static buffer to a dynamic buffer, but ive tried it in ccs and didnt work either....

regards.
_________________
Andre
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group