Solved

two dimensional strings.

Posted on 2004-10-05
4
233 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

Scale it in WD Gold

With up to ten times the workload capacity of desktop drives, WD Gold hard drives employ advanced technology to deliver among the best in reliability, capacity, power efficiency and performance.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
logging Access violation 6 18
How do I install gcc 4.8.4 on a Linux Ubuntu 14.04 machine? 5 1,555
Passing a array as parameter - C 2 84
How to install SVN Command Line Client? 5 39
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

932 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

18 Experts available now in Live!

Get 1:1 Help Now