473,395 Members | 1,368 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,395 software developers and data experts.

pointer to char array

Program : -

#include<iostream.h>
char *fl[5]={0};
void print1();

void main()
{
char p[3]="";
int lng=52;
for (int j=0; j<=strlen("think") ;j++ , lng++ )
{
sprintf(p,"%d",lng);
fl[j]=p;
cout<<"Value in loop is--> " << fl[j]<<endl;
}
print1();
}

void print1()
{
for(int i=0; i<=5; i++)
cout <<"Value after--> "<<fl[i]<<endl;
}



Output : -

./123
Value in loop is--> 52
Value in loop is--> 53
Value in loop is--> 54
Value in loop is--> 55
Value in loop is--> 56
Value in loop is--> 57
Value after--> 57
Value after--> 57
Value after--> 57
Value after--> 57
Value after--> 57
Value after--> 57


Issue : - Statement fl[j]=p; is setting all the elements of the array as the same value , pl help !!
May 12 '12 #1
1 1669
weaknessforcats
9,208 Expert Mod 8TB
You only have one array p.

You assign p to every element of f.

Therefore, the last value you put in p will appear as the value for
every element of f.

You need to allocate a new p array for every element of f. Then you will be able to aev searate values for every f element.
May 13 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Robbie Hatley | last post by:
About 20 days ago, "John Harrison" <john_andronicus@hotmail.com> replied to a message which I had written: > > > Q1 . What is the use of pointer to an array? > > > > It points to it. (The name...
5
by: ali | last post by:
Hi, I'm trying to understand the reason for different output on the following codes Code1: #include <iostream.h> int main()
7
by: Frank | last post by:
Sorry that the question I posted few minutes ago didn't correctly describe the problem. So please ignore it. I repost the question as below: ============================================== I...
12
by: ur8x | last post by:
Why does this declaration give undefined result: file1: extern char * p; file2: char p; Let's assume p has been initialized, now accessing p...
23
by: Leon Brodskiy | last post by:
Hi, Could please anyone clarify about pointer and array in C? If I have: int arr; The following two commands will be the same: arr and &arr.
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 =...
1
by: Jeff | last post by:
I am struggling with the following How do I marshal/access a pointer to an array of strings within a structure Than Jef ----------------------------------------------------------------
8
by: Frank Liebelt | last post by:
Hi I try to convert a int array into a char array. My code: void exec() { char mds; int i; int mdc =...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
7
by: vippstar | last post by:
Today I got a confusing message from gcc (I'm aware, those don't break conformance ;-) In function 'main': 7: warning: format '%d' expects type 'int', but argument 2 has type 'char (*)' The...
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?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.