Solved

two dimensional strings.

Posted on 2004-10-05
4
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

688 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