473,382 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

pointer arithmetic help

Hi there,

I need to copy the first 5 characters stored in a buffer into another
buffer one character at a time. I tried doiing it as under but I got
segmentation errors:
#include<stdio.h>

#define LENGTH 5
char * kernel_buf;
char * user_buf = "Priya is confused\n";

int main(){
int i;
char ch;
printf("user_buf:%s",user_buf);
for(i = 0; i < LENGTH; i++){
*kernel_buf = *(user_buf++);
printf("%c\n",*kernel_buf);
}
return 1;
}

The message I get is:
user_buf:Priya is confused
Segmentation fault

If anyone could help me figure out where I am wrong it would be very
helpful.

Thank you,
Priya

Oct 16 '06 #1
14 2069
priyanka wrote:
Hi there,

I need to copy the first 5 characters stored in a buffer into another
buffer one character at a time. I tried doiing it as under but I got
segmentation errors:
Posting three times isn't a good idea. Usenet isn't synchronous, you
have to wait for posting to propagate.
#include<stdio.h>

#define LENGTH 5
char * kernel_buf;
char * user_buf = "Priya is confused\n";
Make the second const char*, you can't change it.
int main(){
int i;
char ch;
printf("user_buf:%s",user_buf);
for(i = 0; i < LENGTH; i++){
*kernel_buf = *(user_buf++);
You haven't allocated any memory for kernel_buf. See malloc().

Once that is fixed, you'll probably want something like

kernel_buf[i] = user_buf[i];

--
Ian Collins.
Oct 17 '06 #2

priyanka wrote:
Hi there,

I need to copy the first 5 characters stored in a buffer into another
buffer one character at a time. I tried doiing it as under but I got
segmentation errors:

#include<stdio.h>

#define LENGTH 5
char * kernel_buf;
Note that this is a char pointer, and by default is likely set to NULL.
char * user_buf = "Priya is confused\n";
This you've actually given a value, so the user_buf holds the address
of the string "Priya is confused\n".
int main(){
int i;
char ch;
printf("user_buf:%s",user_buf);
for(i = 0; i < LENGTH; i++){
*kernel_buf = *(user_buf++);
What is the benefit to using a pointer to a char instead of a char, in
this scenario? You are making things overly complex, and have ended up
attempting to store a value at an invalid address. (Incidentally, this
is where your segfault will be.)

There are several solutions. You could declare kernel_buf to simply be
a char, and get rid of all your asterisks. You could malloc a char. Or
you could set kernel_buf to point to the char ch you have above.

i.e.

char kernel_buf;
for(i = 0; i < LENGTH; i++){
kernel_buf = *user_buf++;
printf("%c\n",kernel_buf);
}

or

kernel_buf = malloc(sizeof(char));
for(i = 0; i < LENGTH; i++){
*kernel_buf = *user_buf++;
printf("%c\n",*kernel_buf);
}

or

kernel_buf = &ch;
for(i = 0; i < LENGTH; i++){
*kernel_buf = *user_buf++;
printf("%c\n",*kernel_buf);
}
printf("%c\n",*kernel_buf);
}
return 1;
Generally, you return 0 on success.
}

The message I get is:
user_buf:Priya is confused
Segmentation fault

If anyone could help me figure out where I am wrong it would be very
helpful.

Thank you,
Priya
Oct 17 '06 #3
Thank you for your help. I tried according to what you guys told:
#include<stdio.h>

#define LENGTH 5

char *kernel_buf;
char *user_buf = "Priya is confused\n";
kernel_buf = malloc(sizeof(char));

int main(){
int i;
char ch;
printf("user_buf:%s",user_buf);
for(i = 0; i < LENGTH; i++){
*kernel_buf = *(user_buf++);
printf("%c\n",*kernel_buf);
}
return 1;
}

Now I got error telling that there are conflicting types for
kernel_buf.

Thank you,
Priya

On Oct 16, 6:18 pm, "Chris Johnson" <effig...@gmail.comwrote:
priyanka wrote:
Hi there,
I need to copy the first 5 characters stored in a buffer into another
buffer one character at a time. I tried doiing it as under but I got
segmentation errors:
#include<stdio.h>
#define LENGTH 5
char * kernel_buf;Note that this is a char pointer, and by default is likely set to NULL.
char * user_buf = "Priya is confused\n";This you've actually given a value, so the user_buf holds the address
of the string "Priya is confused\n".
int main(){
int i;
char ch;
printf("user_buf:%s",user_buf);
for(i = 0; i < LENGTH; i++){
*kernel_buf = *(user_buf++);What is the benefit to using a pointer to a char instead of a char, in
this scenario? You are making things overly complex, and have ended up
attempting to store a value at an invalid address. (Incidentally, this
is where your segfault will be.)

There are several solutions. You could declare kernel_buf to simply be
a char, and get rid of all your asterisks. You could malloc a char. Or
you could set kernel_buf to point to the char ch you have above.

i.e.

char kernel_buf;
for(i = 0; i < LENGTH; i++){
kernel_buf = *user_buf++;
printf("%c\n",kernel_buf);

}or

kernel_buf = malloc(sizeof(char));
for(i = 0; i < LENGTH; i++){
*kernel_buf = *user_buf++;
printf("%c\n",*kernel_buf);

}or

kernel_buf = &ch;
for(i = 0; i < LENGTH; i++){
*kernel_buf = *user_buf++;
printf("%c\n",*kernel_buf);

}
printf("%c\n",*kernel_buf);
}
return 1;Generally, you return 0 on success.
}
The message I get is:
user_buf:Priya is confused
Segmentation fault
If anyone could help me figure out where I am wrong it would be very
helpful.
Thank you,
Priya
Oct 17 '06 #4
priyanka wrote:
Thank you for your help. I tried according to what you guys told:
Please don't top post, your reply should follow the message you are
relying to.
#include<stdio.h>

#define LENGTH 5

char *kernel_buf;
char *user_buf = "Priya is confused\n";
kernel_buf = malloc(sizeof(char));
This only assigns a buffer of one character to kernel_buf. You should
have this assignment (and the declaration) within main().
int main(){
int i;
char ch;
printf("user_buf:%s",user_buf);
for(i = 0; i < LENGTH; i++){
*kernel_buf = *(user_buf++);
You are still copying all the characters in user_buf to the first
character in kernel_buf.

--
Ian Collins.
Oct 17 '06 #5
priyanka wrote:
Thank you for your help. I tried according to what you guys told:
#include<stdio.h>

#define LENGTH 5

char *kernel_buf;
char *user_buf = "Priya is confused\n";
kernel_buf = malloc(sizeof(char));
This should go in your main(). malloc() is a function, not a constant,
so it has to be called in a method.

I have to say I honestly didn't expect you to use malloc(). It's
generally used in allocating arrays, and is completely unnecessary
here. I think your better choice would be to drop the * on the initial
declaration (and later dereferences) of kernel_buf.

Also, I was just working with what the program indicated you were going
for, which was a single character buffer that iterated through the
first five characters of user_buf. Others seem to be under the
impression that you're wanting to store all five characters at once. If
that's the case, Ian Collins probably gave the most succinct
description of what you'd want to do there.
int main(){
int i;
char ch;
printf("user_buf:%s",user_buf);
for(i = 0; i < LENGTH; i++){
*kernel_buf = *(user_buf++);
printf("%c\n",*kernel_buf);
}
return 1;
}

Now I got error telling that there are conflicting types for
kernel_buf.
I expect it's because you did not include <stdlib.h>, where malloc is
usually defined, so your compiler is guessing what malloc(sizeof(char))
means.
Thank you,
Priya
In the future, reply beneath the context to which you are replying.

<snip>

Oct 17 '06 #6

priyanka wrote:
Hi there,

I need to copy the first 5 characters stored in a buffer into another
buffer one character at a time. I tried doiing it as under but I got
segmentation errors:
#include<stdio.h>

#define LENGTH 5
char * kernel_buf;
char * user_buf = "Priya is confused\n";
char * user_buf = "Priya is confused\n";
replace the above line with,

char user_buf[ ] = "Priya is confused\n";

This will solve your complete problem.
int main(){
int i;
char ch;
printf("user_buf:%s",user_buf);
for(i = 0; i < LENGTH; i++){
*kernel_buf = *(user_buf++);
printf("%c\n",*kernel_buf);
}
return 1;
}

The message I get is:
user_buf:Priya is confused
Segmentation fault

If anyone could help me figure out where I am wrong it would be very
helpful.

Thank you,
Priya
Cheers,
Sandeep.

Oct 17 '06 #7

priyanka wrote:
Hi there,

I need to copy the first 5 characters stored in a buffer into another
buffer one character at a time. I tried doiing it as under but I got
segmentation errors:
#include<stdio.h>

#define LENGTH 5
char * kernel_buf;
I forgot to mention, no memory is allocated for kernel_buf. So, do that
using malloc( ) and terminale the last character with a NUll, else, if
ever you try to print it using %s, you might again get a seg fault.
char * user_buf = "Priya is confused\n";

int main(){
int i;
char ch;
printf("user_buf:%s",user_buf);
for(i = 0; i < LENGTH; i++){
*kernel_buf = *(user_buf++);
printf("%c\n",*kernel_buf);
}
return 1;
}

The message I get is:
user_buf:Priya is confused
Segmentation fault

If anyone could help me figure out where I am wrong it would be very
helpful.

Thank you,
Priya
Cheers,
Sandeep.

Oct 17 '06 #8
"priyanka" <pr**********@gmail.comwrites:
Thank you for your help. I tried according to what you guys told:
#include<stdio.h>

#define LENGTH 5

char *kernel_buf;
char *user_buf = "Priya is confused\n";
kernel_buf = malloc(sizeof(char));

int main(){
int i;
char ch;
printf("user_buf:%s",user_buf);
for(i = 0; i < LENGTH; i++){
*kernel_buf = *(user_buf++);
printf("%c\n",*kernel_buf);
}
return 1;
}

Now I got error telling that there are conflicting types for
kernel_buf.
Your malloc is in the wrong place. It needs to be in main.

For now, and for learning process as I think pointers are confusing you
a little : define kernel_buf as

char kernel_buf[LENGTH+1]; /* zero terminated to be on the safe side */

to start with.

Then in your loop:

kernel_buf[i]=user_buf[i];

After you see this working, maybe then make kernel_buf a local variable in main and try to use
malloc there - it will be back to defining kernel_buf as a "char *"
again. Different from the array notation I used above.

If you use a debugger, experiment with querying the system what value
kernel_buf has, what &kernel_buf[0] is, what *kernel_buf is etc etc -
you will get the hang of it! Most of all, read Kernighan & Ritchies
"The C Programming Language".

best of luck!

Oct 17 '06 #9
sandy wrote:
>
priyanka wrote:
Hi there,

I need to copy the first 5 characters stored in a buffer into
another buffer one character at a time. I tried doiing it as under
but I got segmentation errors:
#include<stdio.h>

#define LENGTH 5
char * kernel_buf;
char * user_buf = "Priya is confused\n";

char * user_buf = "Priya is confused\n";
replace the above line with,

char user_buf[ ] = "Priya is confused\n";

This will solve your complete problem.
How will it have any affect? What is it that you think this solves? You
didn't correct the actual problem, and introduced a new one:
for(i = 0; i < LENGTH; i++){
*kernel_buf = *(user_buf++);
With your change, user_buf is no longer a pointer, and can't be
incremented.


Brian
Oct 17 '06 #10
Chris Johnson wrote:
priyanka wrote:
>Hi there,

I need to copy the first 5 characters stored in a buffer into another
buffer one character at a time. I tried doiing it as under but I got
segmentation errors:

#include<stdio.h>

#define LENGTH 5
char * kernel_buf;

Note that this is a char pointer, and by default is likely set to NULL.
Not just likely. It *must* be set to a null pointer.

--
Simon.
Oct 18 '06 #11

Simon Biber wrote:
Chris Johnson wrote:
priyanka wrote:
Hi there,

I need to copy the first 5 characters stored in a buffer into another
buffer one character at a time. I tried doiing it as under but I got
segmentation errors:

#include<stdio.h>

#define LENGTH 5
char * kernel_buf;
Note that this is a char pointer, and by default is likely set to NULL.

Not just likely. It *must* be set to a null pointer.
Is this because it is declared at file level, or must all new
unassigned pointers be set to null?

Oct 18 '06 #12
Chris Johnson wrote:
>
Simon Biber wrote:
>Chris Johnson wrote:
priyanka wrote:
Hi there,

I need to copy the first 5 characters stored in a buffer into another
buffer one character at a time. I tried doiing it as under but I got
segmentation errors:

#include<stdio.h>

#define LENGTH 5
char * kernel_buf;

Note that this is a char pointer, and by default is likely set to NULL.

Not just likely. It *must* be set to a null pointer.

Is this because it is declared at file level, or must all new
unassigned pointers be set to null?
because it's at the file level.
If it were a local variable in a function or block of code it would not be
initialised.
--
Bill Medland
Oct 18 '06 #13
Bill Medland <bi*********@shaw.cawrites:
Chris Johnson wrote:
>Simon Biber wrote:
>>Chris Johnson wrote:
priyanka wrote:
I need to copy the first 5 characters stored in a buffer into another
buffer one character at a time. I tried doiing it as under but I got
segmentation errors:

#include<stdio.h>

#define LENGTH 5
char * kernel_buf;

Note that this is a char pointer, and by default is likely set to NULL.

Not just likely. It *must* be set to a null pointer.

Is this because it is declared at file level, or must all new
unassigned pointers be set to null?
because it's at the file level.
If it were a local variable in a function or block of code it would not be
initialised.
It's because it has static storage duration. That's the default for
objects declared outside any function. Inside a function, it's too
dark to read -- oop, wrong joke. Inside a function, an object can be
made static by declaring it with the "static" keyword; in that case,
it's initialized as if it were declared at file scope.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 18 '06 #14
priyanka wrote:
Hi there,

I need to copy the first 5 characters stored in a buffer into another
buffer one character at a time. I tried doiing it as under but I got
segmentation errors:
#include<stdio.h>

#define LENGTH 5
char * kernel_buf;
char * user_buf = "Priya is confused\n";

int main(){
int i;
char ch;
printf("user_buf:%s",user_buf);
for(i = 0; i < LENGTH; i++){
*kernel_buf = *(user_buf++);
printf("%c\n",*kernel_buf);
}
return 1;
}

The message I get is:
user_buf:Priya is confused
Segmentation fault

If anyone could help me figure out where I am wrong it would be very
helpful.

Thank you,
Priya
I note that kernel_buff, at file scope, is initialized by the compiler
to NULL, and is left unchanged by your program.

Dereferencing NULL pointer is a no no.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Oct 18 '06 #15

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

Similar topics

23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
11
by: Sushil | last post by:
Hi Gurus I've tried to come up with a small logical example of my problem. The problem is platform specific (MIPS) which I understand should not be discussed here. So here goes my example: ...
6
by: Francois Grieu | last post by:
Are these programs correct ? #include <stdio.h> unsigned char a = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a+j-1 )); return 0; }
3
by: randomtalk | last post by:
hello everyone! Well, recently i've been trying to pick up c and see what is pointer all about (been programming in lisp/python for the better part of my last two years).. mmm.. I'm currently...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
7
by: barikat | last post by:
int a; int *Ptr1, *Ptr2; Ptr1 = a; Ptr1++; Ptr2 = a; printf("Ptr1 : %p\n", Ptr1); printf("Ptr2 : %p\n\n", Ptr2);
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
3
by: =?Utf-8?B?RGlwZXNoX1NoYXJtYQ==?= | last post by:
Hi all, I am porting my code in VC++ to VC.net i.e managed. I have seen somewhere that i need to convert my char* to String*, but String * doesnt perform pointer arithmetic like String* p; *p++='...
19
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.