0
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#define N 8

void func (char* ch,char **c1_a,char **c2_a,int* c1_s,int* c2_s)
{
    char* c2_a;
    int i;
    for(i=0; i<N; i++)
    {
        if(ch[i] >= 'A' && ch[i] <= 'Z')
            (*c1_s)++;
    }
    *c2_s = N - *c1_s;
    *c1_a = (char*)malloc((*c1_s)*sizeof(char));
    *c2_a = (char*)malloc((*c2_s)*sizeof(char));

    *c1_s=*c2_s=0;

    for(i=0;i<N;i++)
    {
        if(ch[i] >= 'A' && ch[i] <= 'Z')
        {
            (**c1_a)[c1_s] = ch[i];
            (*c1_s)++;
        }
        else
        {
            (**c2_a)[c2_s] = ch[i];
            (*c2_s)++;
        }
    }

}

void main ()
{
    char ch[N] = {'A','c','Z','D','G','h','i','Q'};
    char* c1_a,*c2_a;
    int c1_s, c2_s,i;

    func(ch,&c1_a,&c2_a,&c1_s,&c2_s);

    printf("Address for Capital is %d and letters are:\n",c1_a);
    for(i=0;i<c1_s;i++)
    {
        printf("%c",c1_a[i]);
    }
        printf("Address for Small is %d and letters are:\n",c2_a);
    for(i=0;i<c2_s;i++)
    {
        printf("%c",c2_a[i]);
    }



}

I checked with F11 and it says it crashes once I get to the line:

*c1_a = (char*)malloc((*c1_s)*sizeof(char));

I have a project with the same idea only with numbers instead and somehow it works.

Paul R
  • 208,748
  • 37
  • 389
  • 560
Itay Zaguri
  • 43
  • 1
  • 7
  • 5
    [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Nov 03 '15 at 13:45
  • 1
    You have redefined the variable c2_a in t the first line of the function func. It is already defined as parameter. – gmug Nov 03 '15 at 13:49
  • 4
    `(**c1_a)[c1_s]` What do you think this line of code does, and what do you think it is supposed to do? It is nonsense. – Lundin Nov 03 '15 at 13:51

2 Answers2

0

In both main and in your function, you do not initialize your variables. Because they are automatic variables, they can have any value:

In main:

char* c1_a,*c2_a;
int c1_s, c2_s,i;

func(ch,&c1_a,&c2_a,&c1_s,&c2_s);

In func:

*c2_s = N - *c1_s;
*c1_a = (char*)malloc((*c1_s)*sizeof(char));
*c2_a = (char*)malloc((*c2_s)*sizeof(char));

For example, in main c1_s isn't initialized so in func *c1_s has a random value, etc...

I also note that in func you have a local variable char* c2_a. This will override the parameter with the same name in the function heading.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
0
  1. I am surprised that you were able to compile the code that you have shown here. You should have got a redeclaration error for c2_a here (below). Anyway, remove that redeclaration.

    void func (char* ch,char **c1_a,char **c2_a,int* c1_s,int* c2_s)
    {
        char* c2_a;
    

    And just to satisfy my curiosity could you mention the compiler that you are using and the arguments that you are passing to the compiler in the comments.

  2. Initialize c1_s and c2_s. Right now you are working with random numbers i.e. garbage values.

    int c1_s = 0, c2_s = 0;
    
  3. Now coming to the most fascinating part of your code -

     (**c1_a)[c1_s] = ch[i];
    

    That this piece of code not only compiles but runs without throwing an exception shows the weakness (or beauty, if that is how you see it) of C.

    What you intended was -

     (*c1_a)[*c1_s] = ch[i];
    

    By amazing coincidence you make 2 mistakes which cancel each other (by that I mean the error combo passes compilation, but it nevertheless fails to give the desired output).
    Your earlier code is equivalent to -

    char ch;
    int arr[SIZE];
    
    ch[arr] = 'w'; /* (**c1_a) is a 'char', c1_s is a pointer to an integer */
    

    See this to find why your code works in spite of the errors.

Fix these issues and your code will work as expected.

Community
  • 1
  • 1
work.bin
  • 1,078
  • 7
  • 29
  • thanks all ! @work.bin how can i Check what kind of Compiler i got ? Plus, didnt really understand the last thing you said about 2 errors canceling each other, it does give the desired output. why wouldnt it? i just got c1_s and c2_s by reference. – Itay Zaguri Nov 03 '15 at 16:17
  • You wrote `(**c1_a)[c1_s] = ch[i];` but what you need is `(*c1_a)[*c1_s] = ch[i];`. (Look closely at the asterisks). – work.bin Nov 04 '15 at 07:09
  • Regarding the compiler - how are you compiling your code? Are you using some IDE? – work.bin Nov 04 '15 at 07:10
  • cool i got what you meant , thanks ! i use Microsoft Visual C++ 2010 Express , tried to install a newer one once but didnt work well. – Itay Zaguri Nov 04 '15 at 15:28
  • I am surprised that VC++ didn't catch the re-declaration error! Anyway, I hope that you finally got your code working as per intent. – work.bin Nov 05 '15 at 08:34