Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   CAN U TEll me the problem (http://www.go4expert.com/forums/tell-t1264/)

ismitevijay 26Aug2006 15:12

CAN U TEll me the problem
 
hi all,
can anyone tell me where the err is :'

Code: CPP

#include<stdio.h>
void set(char *items[]);

int main()
{
    char *items[]={"apple","pear","banana","grape"};
    set (items);
    return 0;
   
}

void set(char *items[])
{
    printf ("\t%s\n", items[2]);
}

this prog compiles without segmentation fault; and gives "banana " as o/p.

but when i giv
Code: CPP

#include<stdio.h>
void set(char *items[]);

int main()
{
    char *items[10];
    gets(items[2]);
   
    set (items);
    return 0;
   
}

void set(char *items[])
{
    printf ("\t%s\n", items[2]);
}

but when i try to gets the names : i get segementation fault;
plz help

shabbir 26Aug2006 16:03

Re: CAN U TEll me the problem
 
Because you are trying to access an element which you have not initialized. You just have the pointer *items but no space allocated. Use malloc to allocate a buffer and then do gets.

ismitevijay 26Aug2006 16:08

Re: CAN U TEll me the problem
 
yeah i hav rectified that problem and it is working fine . but i was trying to solve another prob and i m getting segmentation fault:

Code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void namesort(char *name[],int n);

int main()
{
        char *name[10];
        char name1[20];
        char *p;
        int n;
        int i;
        int len;
       
        printf("ENTER THE NUMBER OF NAMES-----> ");
        scanf("%d",&n);
       
        for(i=0;i<n;i++)
        {
                printf("\n enter the name:\n ");
                scanf("%s",&name1);
                len=strlen(name1);
                p=malloc(len+1);
                strcpy(p,name1);
                name[i]=strdup(p);
        }
       
        printf("%s \n ",name[1]);
       
        namesort(name,n);
       
        printf(" \n THE NAMES AFTER SORTING \n");
        for(i=0;i<n;i++)
        {
                printf(" %s \n",name[i]);
        }
        printf("\n");
        return 0;
}

void namesort(char *name[],int n)
{
        char temp[20];
        printf(" hi\n");
        printf(" %s ",name[0]);
        int i;
        int j;
        for(i=0;i<n;i++)
        {
                for(j=0;j<(n-1);j++)
                        printf(" %s ",name[j]);
                if(strcmp(name[j],name[j+1])>0)
                       
                {
                        strcpy(temp,name[j]);
                        strcpy(name[j],name[j+1]);
                        strcpy(name[j+1],temp);
                       
                }
               
        }
}

i get segmenattion fault:
plz help

ismitevijay 26Aug2006 16:09

Re: CAN U TEll me the problem
 
ENTER THE NUMBER OF NAMES-----> 2

enter the name:
hy

enter the name:
ugg
ugg
hi
Segmentation fault

this was o/p nd im using gcc compiler.

shabbir 26Aug2006 19:15

Re: CAN U TEll me the problem
 
Because you are comparing if(strcmp(name[j],name[j+1])>0) the values outside the loop with the index j

shabbir 26Aug2006 19:17

Re: CAN U TEll me the problem
 
Also please do use the code bb code for the code snippets you post


All times are GMT +5.5. The time now is 17:44.