Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 16th, 2005, 5:10 AM   #1
conbrio
Newbie
 
Join Date: Apr 2005
Posts: 10
Rep Power: 0 conbrio is on a distinguished road
pointers strings, and strcat()

okay, i'm new to C and going crazy trying to understand this little problem.

in the code below, example A doesn't work, while B does. why is this so?

i thought that declaring strings by "char *blah" and "char blah[]" would essentially be the same since arrays are just pointers anyway. am i wrong?

the reason i ask this is because i'm writing a function that needs to use strings which aren't initialized with a value and hence need to have the array size automatically known ("char *blah;" works for this while "char blah[];" gives an array size unknown error).

after all this, i then want the strings to be passed to strcat() for concatenation.

i really hope someone can help here.. thanks.

// EXAMPLE A
#include <stdio.h>
#include <string.h>



main()
{
   char *yr="1985";
   char *car=" toyota";
   strcat(yr, car);
   printf("%s", yr);
}



// EXAMPLE B
#include <stdio.h>
#include <string.h>


main()
{
   char yr[]="1985";
   char car[]=" toyota";
   strcat(yr, car);
   printf("%s", yr);
}
conbrio is offline   Reply With Quote
Old Apr 16th, 2005, 7:12 AM   #2
BaroN NighT
Programmer
 
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4 BaroN NighT is on a distinguished road
Pointers points to an address of a variable, thus example A will not work because its 1985 and toyota is not an address.
However, example B works because you are declaring a char string to yr[] and car[]. Both 1985 and toyota in this example fills the array.
Arrays are different from pointers, for yr[], yr works like a pointer but it only points to the address of the first element in the string.
BaroN NighT is offline   Reply With Quote
Old Apr 16th, 2005, 11:55 AM   #3
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 579
Rep Power: 5 Benoit is on a distinguished road
Arrays are just pointers in disguise

*(ara+2) is the same thing as ara[2]

Example A didn't work because *yr didn't have enough space to hold *car
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Apr 16th, 2005, 2:23 PM   #4
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>>would essentially be the same since arrays are just pointers anyway
Arrays are not pointers and pointers are not arrays. An array name is converted to a pointer to the first element in many situations, so it may seem that way, but mistaking one for the other results in subtle and dangerous errors.

>>example A doesn't work, while B does. why is this so?
Actually, both are wrong. Example A attempts to modify a string literal and example B tries to copy more data into an array than the array can hold. Here are the differences.

>>char *yr="1985";
yr is a pointer to a string literal. The string literal may be placed in read-only memory, so it is effectively constant. And attempt to modify the memory that yr points to is an error.

>>char yr[]="1985";
yr is an array that was initialized with a string literal. The size of the array-because it was omitted-is determined by the length of the string literal, including the null character. So the length of yr is 5 and it was initialized with {'1','9','8','5','\0'}. Because yr resides within your address space and is not read-only, you can modify it.
Eggbert is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:06 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC