pointer arithmetic help

P

priyanka

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

Ian Collins

priyanka said:
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 = user_buf;
 
C

Chris Johnson

priyanka said:
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.
 
P

priyanka

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
 
I

Ian Collins

priyanka said:
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.
 
C

Chris Johnson

priyanka said:
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>
 
S

sandy

priyanka said:
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.
 
S

sandy

priyanka said:
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.
 
R

Richard

priyanka said:
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=user_buf;

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!
 
D

Default User

sandy said:
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:

With your change, user_buf is no longer a pointer, and can't be
incremented.




Brian
 
C

Chris Johnson

Simon said:
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?
 
B

Bill Medland

Chris said:
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.
 
K

Keith Thompson

Bill Medland said:
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.
 
J

Joe Wright

priyanka said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top