Solved

two dimensional strings.

Posted on 2004-10-05
4
231 Views
Last Modified: 2010-04-15
Hi ,

 I have a structure with array of strings as a field as follows :

 struct transaction
 {
   char *special case;
   .........
  .....
};
transaction trans[20];
  and a global variable called opr which is a char array
char opr[10]="";

Now, this opr gets a line from a file each time a file read is executed and if this line satisfies a special condition then it is stored in the special case field of transaction.

Here is what i did.,
I tested the condition and then
trans[i].specialcase[a]=opr;

where  a is a counter.

Problem is the address of the specialcase[a] is being assigned to opr so that whenever opr gets a new value the specialcase[a] is pointing to that... which means the address of opr and specialcase[a] becomes the same...  I have to get rid of this problem.

I have to only store that special case and the address of both variables should be different. How to do that... Urgent please.
0
Comment
Question by:prashanth_gurijala
4 Comments
 
LVL 5

Expert Comment

by:lwinkenb
ID: 12235027
You dont have an array of strings here, you have an array of pointers.  You need to either change your struct to:

struct transaction
 {
   char specialcasebuff[SOMEBUFFERSIZE];
   .........
  .....
};

or you need to allocate memory to the pointers inside the transaction structures.
trans[i].specialcase = new char[SOMEBUFFERSIZE];

Next, to copy a string into these buffers, you cannot use a equals sign like you had:
trans[i].specialcase[a]=opr;
This only copies the pointer value like you noted in your post.  Instead, use the strcpy() function.
strcpy(trans[i].specialcase,opr);
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 500 total points
ID: 12235298
If you have ONE special case per transaction.

Change your structure to:

struct transaction
 {
   char special case[10];
   .........
  .....
};

Then, where you detect the special case:

strcpy(trans[i].specialcase,opr);

This would copy the special case string into the transaction.
--------------------------------------------------------------------
If you have MANY special cases per transaction.

Change your structure to:

struct transaction
 {
   char special case[100][10];
   .........
  .....
};

Then, where you detect the special case:

strcpy(trans[i].specialcase[a],opr);

--------------------------------------------------------------------
I can think of other interpretations of your question. If the above doesnt help, please give more details.

Paul
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12235635
Either make specialcase an array of char (as Paul suggests),
or make a duplicate of the string in opr:

trans[i].specialcase = strdup(opr);

Just remember to free() the allocated memory when you are through with it.

I can't figure out what you are attempting to do with the index, a , in trans[i].specialcase[a]
specialcase is not an array of strings, it is a pointer to a single string.
(A string is a pointer to char [array of char implied]).


0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 12238370
If you have multiple special cases in a transaction,
I'd suggest a variant of what Paul suggested,

Instead of:
char opr[10];

use:

char opr[100][10];

and use char *specialcase; in the struct,

Now you have all your lines read in from the file stored separately in opr[i],you can make your special case pointer point to any line since you have allocated separate memory for each line.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now