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)[code=c]
#include<stdio. h>
#include<string .h>
#include<math.h >
#include<stdlib .h>
#define MAX(a,b) (a>b?a:b)
static int LCS_length(char **ptr_x,char **ptr_y,char **ptr_c,int m,int n);
static backTrack(char **ptr_c,char **ptr_x,char **ptr_y,int i,int j);
static print(char **ptr_c,char **ptr_x,char **ptr_y, int i, int j);
int main()
{
char **ptr_x,**ptr_y ,**ptr_c;
char x,y;
int m,n;
int i=0,j=0;
int index_i,index_j ,index_c;
printf("Enter the value of m\n");
scanf("%d",&m);
printf("Enter the first string\n");
scanf("%s",x);
strcpy(ptr_x,x) ;[/code] 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:[code=c]
char a[10][10];
char **ptr_x;
ptr_x=a;
printf(enter the value of a);
scanf("%s",a)[/code]
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
#include<stdio. h>
#include<string .h>
#include<math.h >
#include<stdlib .h>
#define MAX(a,b) (a>b?a:b)
static int LCS_length(char **ptr_x,char **ptr_y,char **ptr_c,int m,int n);
static backTrack(char **ptr_c,char **ptr_x,char **ptr_y,int i,int j);
static print(char **ptr_c,char **ptr_x,char **ptr_y, int i, int j);
int main()
{
char **ptr_x,**ptr_y ,**ptr_c;
char x,y;
int m,n;
int i=0,j=0;
int index_i,index_j ,index_c;
printf("Enter the value of m\n");
scanf("%d",&m);
printf("Enter the first string\n");
scanf("%s",x);
strcpy(ptr_x,x) ;[/code] 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:[code=c]
char a[10][10];
char **ptr_x;
ptr_x=a;
printf(enter the value of a);
scanf("%s",a)[/code]
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
Comment