ccharaddressing

C addressing method


#include <stdio.h>
#include <stdlib.h>
#define can 100
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
 void hesaplacumle(int *sayi,char dizi[can])
 {
    *sayi=strlen(dizi);
    printf("Karakter dizimin boyu: %d\n",*sayi);
    
 }
 void ortakhesap(int *ortak,char dizi1[can])
 {
     *ortak=0;
 int i=0;
  int konum=0;
  int num1[26]={0};
  int num2[26]={0};
  while(dizi1[i])
  {
    if(dizi1[i]==32)
      {
        break;
      }
    else if(dizi1[i]>96 && dizi1[i]<123)
    {
        konum=dizi1[i]-'a';
        num1[konum]++;
    }
    i++;
  }
   i+=1;
    while(dizi1[i])
  {
    
    if(dizi1[i]>96 && dizi1[i]<123)
    {
        konum=dizi1[i]-'a';
        num2[konum]++;
    }
    else{
        break;
    }
     
     
    i++;
  }

 
    for(i=0;i<26;i++)
    {
    if(num1[i]!=0 && num2[i]!=0)
    {
        *ortak++;
    }
    }
    printf("2 kelimenin ortak harf sayisi: %d",*ortak);
 }
int main(int argc, char *argv[]) 
{
    int ortak;
  char dizi[100];
  char dizi1[100];
  int sayi;
  printf("\nLutfen bir cumle giriniz: \n");
  gets(dizi);
  hesaplacumle(&sayi,dizi);
  
  printf("\nLutfen iki kelime giriniz: \n\n");
  gets(dizi1);
  
  
  ortakhesap(&ortak,dizi1);
 
  
    
}

hesaplacumle(&sayi,dizi); I was expecting exactly same ouput from this function it is OK.However, ortakhesap(&ortak,dizi1); when I try this one without using function it works properly.On the other hand, when I try it using a function it prodcues same ridiculous outputs which I do not understand.Here my question is what I am missing?Main idea is using addressing method.Thanks for your helps.Due my respects.

hesaplacumle(&sayi,dizi) this one calculates long of the sentences. ortakhesap(&ortak,dizi1); this one calculates common character number between two word. True and wrong output


Solution

  • The problem with function ortakhesap() is here:

            *ortak++;
    

    You intend to increment the int to which ortak points, but postfix ++ has higher precedence than the unary * operator, so you are instead incrementing the pointer itself, then dereferencing it and discarding the value. You could achieve what you intended by inserting parentheses ...

            (*ortak)++;
    

    ... or by using plussignment instead of the increment operator ...

            *ortak += 1;
    

    . Better, however, would be either to use the function's return value instead of an out parameter, or at least to use a local variable to accumulate the count, and set *ortak only once, at the end.