473,507 Members | 3,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why does this not work?

How come this function doesn't work?

void shift(char** list, int index) {
char buff = (*list)[0];
memmove((*list)+1, *list, index);
(*list)[0] = buff;
return;
}

I call it like so:

char* t = "abcde";

shift(&t, 1);

I expect t = "bacde"

or

shift(&t, 0) would not effect t at all.

Anyway, I made it work by creating another character array inside the function.
I then did a memcpy to copy list to this temp buffer and the memmove function
worked correctly when I passed in the temp buffer local to the function. I
would like to understand why the above code will not work. Am I doing something
wrong?

Thanks,
Matt
May 21 '06 #1
5 2028
>How come this function doesn't work?

If you attempt to write on a quoted string literal, you invoke
the wrath of undefined behavior.

What *does* it do on your machine? Smegmentation fault?
void shift(char** list, int index) {
char buff = (*list)[0];
memmove((*list)+1, *list, index);
(*list)[0] = buff;
return;
}

I call it like so:

char* t = "abcde";

shift(&t, 1);

I expect t = "bacde"

or

shift(&t, 0) would not effect t at all.

Anyway, I made it work by creating another character array inside the function.
I then did a memcpy to copy list to this temp buffer and the memmove function
worked correctly when I passed in the temp buffer local to the function. I
would like to understand why the above code will not work. Am I doing something
wrong?


You are attempting to write on a string literal. That's bad.

Gordon L. Burditt
May 21 '06 #2
Gordon Burditt wrote:
How come this function doesn't work?


If you attempt to write on a quoted string literal, you invoke
the wrath of undefined behavior.

What *does* it do on your machine? Smegmentation fault?
void shift(char** list, int index) {
char buff = (*list)[0];
memmove((*list)+1, *list, index);
(*list)[0] = buff;
return;
}

I call it like so:

char* t = "abcde";

shift(&t, 1);

I expect t = "bacde"

or

shift(&t, 0) would not effect t at all.

Anyway,
I made it work by creating another character array
inside the function.
I then did a memcpy to copy list to this temp buffer
and the memmove function
worked correctly when I passed in the temp buffer
local to the function.
I would like to understand why the above code will not work.
Am I doing something
wrong?


You are attempting to write on a string literal. That's bad.


Also,
the function is overly complicated
concerning levels of indirection,
and bear in mind that it is undefined
if strlen(t) is less than 2.

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

void shift(char* list, size_t index);

int main (void)
{
char t[] = "abcde";

shift(t, 1);
puts(t);
return 0;
}

void shift(char* list, size_t index)
{
char buf = list[1];

memmove(list + 1, list, index);
list[0] = buf;
}

/* END new.c */

--
pete
May 21 '06 #3
Gordon Burditt wrote:
How come this function doesn't work?

If you attempt to write on a quoted string literal, you invoke
the wrath of undefined behavior.

What *does* it do on your machine? Smegmentation fault?

void shift(char** list, int index) {
char buff = (*list)[0];
memmove((*list)+1, *list, index);
(*list)[0] = buff;
return;
}

I call it like so:

char* t = "abcde";

shift(&t, 1);

I expect t = "bacde"

or

shift(&t, 0) would not effect t at all.

Anyway, I made it work by creating another character array inside the function.
I then did a memcpy to copy list to this temp buffer and the memmove function
worked correctly when I passed in the temp buffer local to the function. I
would like to understand why the above code will not work. Am I doing something
wrong?

You are attempting to write on a string literal. That's bad.

Gordon L. Burditt


It generated a segmentation fault. I thought that I would need to pass in a
poniter to the string that I was modifying for the changes to effect to the
string outside the function.

I'm going to try the code that pete posted to see if it will work. I still
don't understand why my function wasn't working. Could you explain writing on a
string literal?

Assuming pete's code works with this function signature:

void shift(char* list, size_t index)

why would it fail with my signature? I'm just passing a pointer to the string.

void shift(char** list, size_t index)?

/* BEGIN pete's code */

#include <stdio.h>
#include <string.h>

void shift(char* list, size_t index);

int main (void)
{
char t[] = "abcde";

shift(t, 1);
puts(t);
return 0;
}

void shift(char* list, size_t index)
{
char buf = list[1];

memmove(list + 1, list, index);
list[0] = buf;
}

/* END pete's code /*
May 21 '06 #4
Gordon Burditt wrote:
How come this function doesn't work?


If you attempt to write on a quoted string literal, you invoke
the wrath of undefined behavior.

I'm sorry, I don't know who you are talking to because don't have
proper attributions. Please try to be responsible participant in the
newsgroup.


Brian

May 21 '06 #5
On Sun, 21 May 2006 00:07:40 -0700, Matt Kowalczyk wrote:
Could you explain writing on a
string literal?


It is simply part of the specification of the language that you may not
modify a string literal.

char *s = "abcde";
s[1] = 'x'; /* not allowed: t points to a string literal */

char t[] = "abcde"; /* the array t is initialised with "abcde" */
t[1] = 'x'; /* OK because t is an ordinary array */

--
Ben.
May 21 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4838
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As...
3
3728
by: Julian | last post by:
Hi I am trying to update a date field in my table but some how this simple code does not work, I know the select work because if I write the fields, it will show the data from the table but why...
5
3595
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
22
2570
by: Robert Bralic | last post by:
CAN anybody tell me any address where I can download some small(1000-2000) lines C++ proghram source. Or send me ,a small(1000-2000) lines C++ program source that I can compille with gpp under...
12
2916
by: Frank Hauptlorenz | last post by:
Hello Out there! I have a DB2 V7.2 Database (Fix11) on Win 2000 Professional. It was before a NT 4 based Domain - now it is a Win 2000 Domain. The database server is a domain member. Now...
0
2343
by: Jarod_24 | last post by:
How does tabindex work in ASP .net pages I dosen't seem to work quite like in regular forms. and there isn't any TabStop property either. 1 .How do you prevent a control form beign "tabbed"....
14
4815
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
89
5948
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
14
3450
by: webEater | last post by:
I have a problem, it's not browser specific, and I don't get a solution. I have an (X)HTML document, I show you a part of it: .... <!--<div class="pad">--> <div id="eventImages"><img src=""...
1
7078
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
5039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1537
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.