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

Least Common Subsequence

I have currently been trying to code the Least Common Subsequence Algorithm in C and I am having some basic problems with pointers and arrays. First of all, how do I read in a string and then pass that string to the pointer? To be more clear I will paste a portion of my code (it's not working yet)
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<stdlib.h>
  5. #define MAX(a,b) (a>b?a:b)
  6.  
  7.  
  8. static int LCS_length(char **ptr_x,char **ptr_y,char **ptr_c,int m,int n);
  9. static  backTrack(char **ptr_c,char **ptr_x,char **ptr_y,int i,int j);
  10. static print(char **ptr_c,char **ptr_x,char **ptr_y, int i, int j);
  11.  
  12.  
  13. int main()
  14. {
  15.     char **ptr_x,**ptr_y,**ptr_c;
  16.     char x,y;
  17.     int m,n;
  18.     int i=0,j=0;
  19.     int index_i,index_j,index_c;
  20.     printf("Enter the value of m\n");
  21.     scanf("%d",&m);
  22.     printf("Enter the first string\n");
  23.     scanf("%s",x);
  24.     strcpy(ptr_x,x);
ptr_x is the pointer variable and its pointing to a 2 D array. I need to read in a string and let this ptr_x point to it.
If i was not using dynamic memory allocation, I would do it as follows:
Declare an array:
Expand|Select|Wrap|Line Numbers
  1. char a[10][10];
  2. char **ptr_x;
  3. ptr_x=a;
  4. printf(enter the value of a);
  5. scanf("%s",a)
I am stuck here, please help me what and how i should use pointers and arrays together when using 2D arrays and dynamic memory allocation.

I am getting these errors when I compile
lcs.c:24: warning: passing argument 1 of âstrcpyâ from incompatible pointer type
lcs.c:24: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast
lcs.c:35: warning: passing argument 1 of âstrcpyâ from incompatible pointer type
lcs.c:35: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast


Thanks in advance!
Anabelle
Jul 18 '07 #1
5 3495
weaknessforcats
9,208 Expert Mod 8TB
Firstly, this code won't compile:
int main()
{
char a[10][10];
char **ptr_x;
ptr_x=a; <<<<<<!!!!!
printf(enter the value of a);
scanf("%s",a)
}
You cannot assgn a (an array) to a pointer-to-pointer to an int.

The reason is that the name of an array (in this case a) is the address of element 0. Element 0 if the array is an array of 10 char.

Therefore, a[0] is an array of 10 char. Therefore the name a is the address of an array of 10 char:

char (*ptr_x)[10] = a; //OK.

A char** is a pointer to a pointer to a single int.

Since you need to read in a string, why not just delcare an array large enough to hold the string:

char array[100];

and scanf() into that??

Later you can treat this array as a 10x10.
Jul 18 '07 #2
I am dynamically allocating memory to the array which is pointed to by the pointer ptr_x. Now my question is, how do I read in a string value from stdin and store it in the **ptr_x? I do not want to do this:

char a[10][10];
char **ptr_x;
ptr_x=(int **) (malloc sizeof(int *)) //allocate memory to ptr_x.
printf("Enter a string value for a\n");
scanf("%s",a);
ptr_x=a; // I don't think this works!
This is the part where I am stuck.
I am not sure how to accept string input from the user and store it into a 2D array pointed to by a pointer!

Thanks,
Anabelle
Jul 18 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
You are making this too hard.

There are no multi-dimensional arrays on C. There are only one-dimensional arrays.

Assume you have this in memory:

RosesAreRedVioletsAreBlue

There are 25 characters there.

A char array[25] in memory would look like:

RosesAreRedVioletsAreBlue

A char array[5][5] in memory would look like:

RosesAreRedVioletsAreBlue

Kinda the same, right?

So, you allocate as for a one-dimensional array. In your case, allocate 25 chars.

Read in your string.

To access the V of Violets you could:

array[11];

If this was 2D array of [5][5] then you want array[2][1] or

array[ 2* 5*sizeof(char) + 1 * 1 * sizeof(char)]

Otherwise written as array[2][1].

It's just a question of who does the pointer aritmetic.

So starting with char array[25] and you string in there

RosesAreRedVioletsAreBlue

You could declare a pointer to an array of 5 char:

char (*ptr_x)[5];

and then typecast the array name to the address of an array of 5 char.

ptr_x = (char (*)[5])array;

Now if you use array, it's one dimensional of 25
If you use ptr_x it's two-dimensional. Each element is an array of 5 char.

and the memory you are using is the same memory.
Jul 18 '07 #4
donthi
1
1. #include<stdio.h>
2. #include<string.h>
3. #include<math.h>
4. #include<stdlib.h>
5. #define MAX(a,b) (a>b?a:b)
6.
7.
8. static int LCS_length(char **ptr_x,char **ptr_y,char **ptr_c,int m,int n);
9. static backTrack(char **ptr_c,char **ptr_x,char **ptr_y,int i,int j);
10. static print(char **ptr_c,char **ptr_x,char **ptr_y, int i, int j);
11.
12.
13. int main()
14. {
15. char **ptr_x,**ptr_y,**ptr_c;
16. char x,y;
17. int m,n;
18. int i=0,j=0;
19. int index_i,index_j,index_c;
20. printf("Enter the value of m\n");
21. scanf("%d",&m);
22. printf("Enter the first string\n");
23. scanf("%s",x);
24. strcpy(ptr_x,x);






can u write reaming code for it
Nov 11 '08 #5
qartar
12
Given your code:

char a[10][10];
char **ptr_x;

Like cats said, your multi-dimensional array is essentially interpreted as an single dimensional array with a size of 100; as in it is a continuous block of 100 characters. Your ptr_x is pointing to a pointer to an array. So when you use array operators [] with ptr_x it expects to find an array of pointers which would then point to your character array. But your multi-dimensional array just has the 100 characters in it, not the array of pointers that ptr_x expects. Maybe that is helpful? :/
Nov 11 '08 #6

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

Similar topics

5
by: Steven T. Hatton | last post by:
I often encounter statements in documentation or comments in source code that says things along the lines of "not all compilers support X, so we did not use it." X might be namespaces,...
52
by: onsbomma | last post by:
I want to set and reset the least significant bit of a address (where a pointers points to). I tried this, but it is not correct: #define BIT 0x1 void foo(){ void *p; *p = *p & ~BIT
5
by: Sebastian | last post by:
Here's the thing I have a web aplication (1.1) and one of my classes has an array as a property (let's say Class A, has an ArrayList of Bs). On the other side I have a win form app which uses web...
10
by: Rich Sienkiewicz | last post by:
The C# compiler gives warnings about unused variables. I thin it needs a warning about an unassigned reference. For instance, this compiles without any warnings/errors, but of course blows up when...
12
by: begum | last post by:
in this program they want me to find the max value of a subsequence in a given sequnce of numbers(the sequence is represented by an array) by using templates and pointers.also i must write a main...
2
by: begum | last post by:
i write a program about finding the max value of a subseqence in a given sequence. i write but i finf lots of syntax mistakes and i can't do them in correct. please help me. Thanks; Begum...
2
by: petermichaux | last post by:
Hi, It seems like determining element position in a web page is a difficult task. In the position reporting source code I've looked at there are special fixes for at least some versions of...
22
by: David Mathog | last post by:
One thing that keeps coming up in this forum is that standard C lacks many functions which are required in a workstation or server but not possible in an embedded controller. This results in a...
6
by: Peter | last post by:
Hi I have a number of arrays of longs, from which I need to find a single array which only contains the values which appear in all the original arrays. For example, I could have the three...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.