0

i have the following problems in C programming.

I have an array of strings stored as words[10][50]. I want to extract each of the string from the array and then pass it on to another function. I tried on the following:

 #include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"

int Check_Anagram(char*,char*);

void main()
{
    char words[10][20];
    int i;
    int flag;
    for(i=0;i<3;i++)
    {
        scanf("%s\n",words[i][20]);
    }
    for(i=1;i<10;i++)
    {
        flag = Check_Anagram(words[i][20],words[i-1][20]);      
    }
    getch();
}

int Check_Anagram(char *a,char *b)
{
    printf("%s %s\n",a,b);
    return 1;
}

This creates an exception during compiling. Now i think that when i use the "printf" statement then this nomenclature works fine i.i words[i] prints the string "i" from the double dimension words array. When i try to do the same thing with the check function then the error occurs.

Can soemone point me how to do this passing ?

P.S. Please ignore any error in efficiency of program and likewise. I need your help and this is just a test program at learning string passing to a function Thanks

4
  • How about posting all of the code? Without seeing how words is initialized, assuming it is initialized, there's not much that can be said. Commented Feb 20, 2012 at 8:33
  • Error 2 error C2664: 'Check_Anagram' : cannot convert parameter 1 from 'char' to 'char *' c:\users\sg0214275\documents\visual studio 2010\projects\anagram\anagram\anagram.cpp 22 1 anagram Also there is some memory referencing error involving some hexadecimal addresses ill post the code here too Commented Feb 20, 2012 at 8:35
  • Could you provide the code of check() function? And neat formatting is always appreciated. Commented Feb 20, 2012 at 8:36
  • I'm already coughing my lungs up, get that void main() out of here. Commented Feb 20, 2012 at 8:37

4 Answers 4

4

You're passing words[i][20]. You need to pass words[i] instead in both loops. Try this:

for(i = 1; i < 3; i++) /* i < 3 */
{
    flag = Check_Anagram(words[i], words[i-1]);
}

Another problem is that you're reading 3 strings and trying to print 10. So when you pass words[3] it contains garbage: printf tries to print garbage which need not be 0-terminated.

Sign up to request clarification or add additional context in comments.

1 Comment

Unhandled exception at 0x5c4fea5f (msvcr100d.dll) in anagram.exe: 0xC0000005: Access violation writing location 0xffffffcc. i get this error on doing that. I had tried it earlier too...didnt work
2

In the first for loop, when i is 0, you're pointing to words[-1], that's your exception.

Comments

1

flag = Check_Anagram(words[i][20],words[i-1][20]);

You are passing the 21st letter of each word the Check_Anagram. Instead you should pass the words themselves:

flag = Check_Anagram(words[i],words[i-1]);

You have a similar problem where you use scanf. To read a line from the console to each word you would use:

for(i=0;i<10;i++)
{
    scanf("%s\n",words[i]);
}

2 Comments

Unhandled exception at 0x5c4fea5f (msvcr100d.dll) in anagram.exe: 0xC0000005: Access violation writing location 0xffffffcc. i get this error on doing that. I had tried it earlier too...didnt work
yeah i did.i got the solution. thanks for all of your helps. will post the correct code as soon as i am allowed to
0
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"

int Check_Anagram(char [],char []);

void main()
{
    char words[10][20];
    int i;
    int flag;
    for(i=0;i<3;i++)
    {
        scanf("%s\n",words[i]);
    }
    for(i=1;i<10;i++)
    {
        flag = Check_Anagram(words[i],words[i-1]);      
    }
    getch();
}

int Check_Anagram(char a[],char b[])
{
    printf("%s %s\n",a,b);
    return 1;
}

I finally got it corrected thanks to the help of all users. I have posted the corrected code for people who are struggling with passing of string extracted from an array of strings to another function. hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.