|
|
View previous topic :: View next topic |
Author |
Message |
cchappyboy
Joined: 03 Dec 2008 Posts: 45
|
Something about pointer in CCS C |
Posted: Thu Nov 19, 2009 9:58 am |
|
|
I made a two-dimensional array
Code: |
char comm[4][6]={"How are you?","I am fine.","Thank you!","You are welcome!"}
|
and a pointer variable int *p;
How can I give the first address of comm[0][0] to p ?
I try make *p=comm[0][0] or p=&comm[0][0] but it does not work.
I heard about some of C can use int (*p)[4] as a pointer of two-dimensional array, but it looks CCS C doesn't do that.
Please give me some idea about this.
Any concern I appreciate in advance. Thanks. |
|
|
Kamputty Guest
|
|
Posted: Thu Nov 19, 2009 11:12 am |
|
|
Here is a working example...
Notice the "char *p"...
You need a pointer to a char, not a pointer to an "int"...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <stdio.h>
int main (int argc, char *argv[])
{
char comm[][50]={"How are you?","I am fine.","Thank you!","You are welcome!"};
char *p;
int x;
for(x=0; x<4; x++)
{
p=comm[x];
printf("The message is [%s]\n", p);
}
return(0);
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 19, 2009 2:40 pm |
|
|
To Kamputty:
Quote: |
#include <stdio.h>
int main (int argc, char *argv[])
{
char comm[][50]={"How are you?","I am fine.","Thank you!","You are welcome!"};
char *p;
int x;
|
Your code doesn't compile in CCS (PCH vs. 4.099). Also, CCS doesn't
support argc or argv. You can't "return" from main(). There is no O/S
to return to.
-------------------------------------------------------
To cchappyboy:
Here is an example of how to get a pointer to individual elements
in a two dimensional text array. The program below has the
following output:
Quote: |
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Last Test: Wednesday
|
Code: |
#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
char weekday_names[7][10] =
{
{"Sunday"},
{"Monday"},
{"Tuesday"},
{"Wednesday"},
{"Thursday"},
{"Friday"},
{"Saturday"}
};
void display_text(char *ptr)
{
printf("%s \r", ptr);
}
//======================================
void main ()
{
int8 i;
char *p;
// Pass a pointer to each array element.
// Display all of them.
for(i=0; i < 7; i++)
{
display_text(weekday_names[i]);
}
// Get a pointer to one element in the array.
// Display it.
i = 3;
p = weekday_names[i];
printf("Last Test: %s", p);
while(1);
}
|
This was tested with PCH vs. 4.099. |
|
|
cchappyboy
Joined: 03 Dec 2008 Posts: 45
|
|
Posted: Thu Nov 19, 2009 4:59 pm |
|
|
Thanks for you guys' help.
My ccs version is 4.066
This time I try this:
Code: |
char comm[4][20]={"How are you?","I am fine.","Thank you!","You are welcome!"};
char *p;
p=comm[0];
printf("%s", p);
|
Pass the build procedure but it don't work.
But works at
Code: |
printf("%s",comm[0]);
|
What is the problem? Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 19, 2009 5:49 pm |
|
|
Post your PIC. Always do this. |
|
|
|
|
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
|