Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-16-2005, 07:50 PM
|
#1
|
|
Member
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170
Rep:
|
c array of pointers
I can assign a new character to my array of strings:
Code:
int main()
{
char *days[] = {"monday", "tuesday", "wednesday", "thursday",
"friday", "saturday", "sunday"};
days[0][0] = 'v'; /* here I can aisgn */
int size = sizeof(days) / sizeof(char *);
int i;
for (i = 0; i < size; i++)
printf("%s\n", *(days + i));
tableReverse(days, size);
but after I pass a pointer to tableReverse() function and I try to assign something I get a segmentation fault.
Code:
void tableReverse(char *table[], int size)
{
table[0][0] = 'q'; /* here I get a segmentation fault */
Why is it like that?
|
|
|
|
08-16-2005, 09:43 PM
|
#2
|
|
LQ Newbie
Registered: Aug 2005
Location: Jiangsu Nanjing, China
Distribution: slackware-current
Posts: 9
Rep:
|
days[0][0] = 'v'; /* here I can aisgn */
<===
Can not assign too.
The address of days[0][0] is the same to which of table[0][0] in function tableReverse() and it is located in the read-only memory area. If write to this area, there will be Segmentation fault
Last edited by sxzzsf; 08-16-2005 at 09:49 PM.
|
|
|
|
08-16-2005, 11:22 PM
|
#3
|
|
Member
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170
Original Poster
Rep:
|
Quote:
Originally posted by sxzzsf
days[0][0] = 'v'; /* here I can aisgn */
<===
Can not assign too.
The address of days[0][0] is the same to which of table[0][0] in function tableReverse() and it is located in the read-only memory area. If write to this area, there will be Segmentation fault
|
I double checked and I'm sure that I can assign in the first case. No segmentation fault and characters get changed.
|
|
|
|
08-16-2005, 11:32 PM
|
#4
|
|
Member
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 213
Rep:
|
How big is char * days[]
How big is char * table[]
Check your allocations.
|
|
|
|
08-16-2005, 11:36 PM
|
#5
|
|
LQ Newbie
Registered: Aug 2005
Location: Jiangsu Nanjing, China
Distribution: slackware-current
Posts: 9
Rep:
|
void tableReverse(char *table[], int size)
{
printf("0x%8x\n",&table[0][0]);
}
int main()
{
int size,i;
char *days[] = {"monday","tuesday", "wednesday","thursday","friday", "saturday", "sunday"};
size = sizeof(days) / sizeof(char *);
for (i = 0; i < size; i++)
printf("%s\n", *(days + i));
tableReverse(days, size);
printf("0x%8x\n",&days[0][0]);
printf("0x%8x\n",&days[0]);
return 0;
}
=============
after compiling using gcc 3.3.1
monday
tuesday
wednesday
thursday
friday
saturday
sunday
0x 804852f <== days[0][0]
0x 804852f <== table[0][0]
0xbffff8f0 <== days
|
|
|
|
08-17-2005, 02:09 AM
|
#6
|
|
Member
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170
Original Poster
Rep:
|
OK, that proves that the pointer(address) is the same. But I still don't understand why I can assign to days[0][0] and not to table[0][0].
|
|
|
|
08-17-2005, 02:23 AM
|
#7
|
|
LQ Newbie
Registered: Aug 2005
Location: Jiangsu Nanjing, China
Distribution: slackware-current
Posts: 9
Rep:
|
But I can not
|
|
|
|
08-17-2005, 04:36 AM
|
#8
|
|
LQ Newbie
Registered: Aug 2005
Posts: 2
Rep:
|
segmentation fault
looks like you got it all wrong you cannot assign the value at either of the places...the segementation fault comes at the statement
days[0][0] = 'v';
when a array of pointers is initialised it becomes a constant pointer and you cannot change the value....
|
|
|
|
08-17-2005, 07:37 AM
|
#9
|
|
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Rep:
|
I also don't see how you are possibly not getting a segmentation fault on the first assignment. Just to be sure, I built it on my system and in fact do get a segmentation fault with just the following code:
Code:
int main()
{
char *days[] = {"monday", "tuesday", "wednesday", "thursday",
"friday", "saturday", "sunday"};
days[0][0] = 'v'; /* here I can aisgn */
return 0;
}
The problem here is you are attempting to change the value of a const char array ("monday" for example, is a const char array). I'm guessing you'd have more luck with something like this:
Code:
int main()
{
char *days[] = {"monday", "tuesday", "wednesday", "thursday",
"friday", "saturday", "sunday"};
int size = sizeof(days) / sizeof(char *);
int i;
for (i = 0; i < size; i++)
printf("%s\n", *(days + i));
days[0] = "blahday"; /* here I can aisgn */
for (i = 0; i < size; i++)
printf("%s\n", *(days + i));
return 0;
}
Last edited by jtshaw; 08-17-2005 at 07:40 AM.
|
|
|
|
08-17-2005, 09:38 AM
|
#10
|
|
Member
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170
Original Poster
Rep:
|
Ok, so I can't assign it.
You, guys are correct.
I pasted the line 'days[0][0] = 'v'; /* here I can aisgn */' in a wrong spot in my post.
Actually I was assigning after calling reverseTable(), where memory is allocated for the reversed strings. I didn't know at first that the first declaration of 'days' makes it constant though.
So this is the program.
Code:
#include <stdlib.h>
#include <stdio.h>
void tableReverse(char *table[], int size);
int main()
{
char *days[] = {"monday", "tuesday", "wednesday", "thursday",
"friday", "saturday", "sunday"};
// days[0][0] = '!'; segmentation fault here
int size = sizeof(days) / sizeof(char *);
int i;
for (i = 0; i < size; i++)
printf("%s\n", *(days + i));
tableReverse(days, size);
days[0][0] = '&'; // can assign here
days[1][0] = '*';
days[2][1] = '$';
for (i = 0; i < size; i++)
printf("%s\n", *(days + i));
return EXIT_SUCCESS;
}
void tableReverse(char *table[], int size)
{
// table[0][0] = 'q'; segmentation fault here
int j;
for (j = 0; j < size; j++){
int len = strlen(*(table + j));
char *str = (char *)malloc(len);
int i;
for (i = 0; i < len; i++)
str[i] = *(*(table + j) + (len - 1 - i));
str[len] = '\0';
*(table + j) = str;
}
}
|
|
|
|
08-17-2005, 10:25 AM
|
#11
|
|
Member
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895
Rep:
|
Whenever you have a string literal (e.g. "monday", "tuesday", etc.) that string literal is stored in a READ-ONLY data section of your executable. You should NEVER, EVER attempt to write to this memory.
If instead, you declared your array of days as a multidimensional array instead of an array of pointers, then memory that can be written to will be declared for you on the stack...
e.g.
Code:
char days[7][10] = {"monday", "tuesday", "wednesday", "thursday",
"friday", "saturday", "sunday"};
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:37 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|