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 support@ccsinfo.com

printf and strings inside structures

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
EdWaugh



Joined: 07 Dec 2004
Posts: 127
Location: Southampton, UK

View user's profile Send private message

printf and strings inside structures
PostPosted: Thu Feb 17, 2005 7:20 am     Reply with quote

Hi all,

I'm having a bit of trouble with some code that I think is to do with pointers. Its perhaps more of a general problem with my C than a specific ccs one. I'm trying to write strings constant and other wise into structs. For example:

Code:

struct example
{
    char string[16];
};

main()
{

struct example instance;

for(i=0;i<15;i++)
{
      instance.string[i] = 'c';
}

printf("%s",instance.string);

}

I realise all the code is not there but I hope you get the idea about what I want to do. I also want to:
Code:

strcopy(instance.string,"hello");

and use them from functions they are passed too:
Code:

fn(struct example *instance)
{
   printf("%s",instance->string)
}

Is this possible or am I making it up? It looks very reasonable to me but doesn't seem to work. I just print blanks over whatever else I was trying to print nearby. I suspect I am pointing at the wrong thing somehow as the string is a pointer inside the structure.

Anyway, any advice would be greatly appreciated I can't seem to find a good note on this subject.

cheers

ed
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 17, 2005 1:48 pm     Reply with quote

Here is an example program that will show you how to do it.
The output of this program is:

1 2 3 Hello

I used PCM vs. 3.218 and 3.219 to test this. I used the MPLAB 7.01
simulator and directed the RS232 output to "UART1" which then
displays it in the Output window.

Code:
#include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

typedef struct
{
char a;
char b;
char c;
char string[10];
}ABC;

ABC test;

void func1(ABC *abc_struct);

//--------------------------------------------

void main()
{

test.a = 1;
test.b = 2;
test.c = 3;
strcpy(test.string, "Hello");

func1(&test);

}

//==========================================
// FUNCTIONS

void func1(ABC *ptr)
{

printf("%d %d %d %s\n\r", ptr->a, ptr->b, ptr->c, ptr->string);
}
EdWaugh



Joined: 07 Dec 2004
Posts: 127
Location: Southampton, UK

View user's profile Send private message

cheers
PostPosted: Fri Feb 18, 2005 4:56 am     Reply with quote

Hi,

I have just tried a simple example at the start of my main function like the one you suggest and that does seem fine. I don't suppose you know if there if there is anything else I should be aware of to do with this? My structs and functions are several calls deep in my program and I'm using compiler version 3.172. I'm also writing to the strings like this:
Code:

fn(struct example *ptr)
{
strcpy(ptr->string,"hello")
}

Which I'm assuming is ok too. Its a very odd problem and hard to simulate as all my data I'm working on comes in over SPI!

I'll keep trying its driving me a bit nuts now.

cheers

ed
Ttelmah
Guest







PostPosted: Fri Feb 18, 2005 8:19 am     Reply with quote

The obvious thing that might be causing problems in the original example, is that a 16 character array, can only hold a 15 character string. Remember a 'string', has a null terminator...

Best Wishes
EdWaugh



Joined: 07 Dec 2004
Posts: 127
Location: Southampton, UK

View user's profile Send private message

hi
PostPosted: Fri Feb 18, 2005 9:47 am     Reply with quote

yea i know about that and have tried making my strings plenty big but this doesn't help either!

cheers

ed
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

Re: hi
PostPosted: Fri Feb 18, 2005 9:55 am     Reply with quote

EdWaugh wrote:
yea i know about that and have tried making my strings plenty big but this doesn't help either!

cheers

ed


Well this doesn't do that
Code:
struct example
{
    char string[16];
};

main()
{

struct example instance;

for(i=0;i<15;i++)
{
      instance.string[i] = 'c';
}

printf("%s",instance.string);

}


Making them big enough isn't the only thing. If you are loading characters into an array to create a string then YOU must terminate the string with a NULL yourself.
EdWaugh



Joined: 07 Dec 2004
Posts: 127
Location: Southampton, UK

View user's profile Send private message

PostPosted: Fri Feb 18, 2005 9:58 am     Reply with quote

yep, ok your right, you mean a '\0' as the final char. I think maybe I'd been looking at strcpy which adds one for you and not been putting one on myself.

cheers

ed
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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