473,466 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pointer question

Hello.
I am trying to learn concept of pointers in C

I wrote a simple int copy program which basically copies the two int
arrays

char *mystrcpy( int *source, int *dest, int no)
{
printf("the first element of char is %d \n", *source);
int i;

for (i=0;i<4;i++)
{
*dest++ =*source++;
}

return dest;
}

int a[5]={2,3,4,5};
int i,j;
//char *ptrA;
//char *ptrB;
int b[5];
ptrA=a;
ptrB=b;
ptrB=mystrcpy (ptrA ,ptrB,3);
for (i=0;i<4;i++)
{
printf("the b array is %d \n",b[i]);
}

So i would expect the output of this program to be
the b array is 2
the b array is 3
the b array is 4
the b array is 5
but i get the following output
the b array is 134518416
the b array is 3
the b array is 4
the b array is 5

Can someone tell me why the first element is messed up

thanks

Sep 9 '07 #1
1 1193
am******@gmail.com writes:
Hello.
I am trying to learn concept of pointers in C

I wrote a simple int copy program which basically copies the two int
arrays
Better to post a program. These fragments just give an idea of what
is wrong.
char *mystrcpy( int *source, int *dest, int no)
^^^^
You probably mean int. The name reveals that this function once did
something else.
{
printf("the first element of char is %d \n", *source);
int i;

for (i=0;i<4;i++)
{
*dest++ =*source++;
}

return dest;
Your compiler whoudl have something to say about returning an int *
when the function is declared to return a char *. If you did not get
a message about this, you need to ask for more help from you compiler
(turn the error level as high as possible).

Note, that dest (the value you return) now points four elements furth
along the array than the parameter that was passed in.
}

int a[5]={2,3,4,5};
int i,j;
//char *ptrA;
//char *ptrB;
int b[5];
ptrA=a;
ptrB=b;
ptrB=mystrcpy (ptrA ,ptrB,3);
Your compiler should have told you about this too. There are two
errors here. You can't assign to an array (b) and the return type of
mystrcpy is wrong. Don't ignore you compiler's help.
for (i=0;i<4;i++)
{
printf("the b array is %d \n",b[i]);
}

So i would expect the output of this program to be
the b array is 2
the b array is 3
the b array is 4
the b array is 5
but i get the following output
the b array is 134518416
the b array is 3
the b array is 4
the b array is 5

Can someone tell me why the first element is messed up
It is not. I can't think how you got this code though a compiler, but
whatever you did, the return from your function is not a pointer to
the copied data. It points past it (see above). To get a more
detailed answer, you'd have to post a program that compiles.

--
Ben.
Sep 9 '07 #2

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

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
17
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This...
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...
9
by: Cyron | last post by:
Hello friends, Recently I have begun exploring the features that the STL map collection provides. While learning how it worked, I encountered a result that confused me. Up until now, I had...
5
by: mdh | last post by:
Hi all, I have gone through the FAQ and done searches in the Comp.Lang and if I have missed it please let me have the ref. (Question generated in part by p119). Given char a;
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
2
by: Giorgos Keramidas | last post by:
On Sun, 05 Oct 2008 18:22:13 +0300, Giorgos Keramidas <keramida@ceid.upatras.grwrote: My apologies. I should have been less hasty to hit `post'. If showtext() is passed a null pointer, it may...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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
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
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 ...

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.