Thread: pointer doubt

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    1

    pointer doubt

    i am posting two programs.The first one is test3.c which is inputing students name and marks.In this one ,the function is getting input correctly but the result is not reflected in the main function.Dont know why.

    In the second one(1_5.c) m unable to replace the spaces in my programs by %20.suppose i enter 'ankur modi' it shud print 'ankur%20modi'.Why this isnt workin.Please modify the programs for error and do let me know the errors you encountered


    PSlease tell me whats wrong with my implementation or concept

    test3.c
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define STUDENTS 2
    #define SUBJECTS 3
    
    
    int main()
        {
        int i,j;
        char name[STUDENTS][20]={'\0'};
        int marks[STUDENTS][SUBJECTS+1]={0};
        inputmarks(name,marks);
        for(i=0;i<STUDENTS;i++)
            {
            printf("\n%s\n",name[i]); //here its not displaying correct
            for(j=0;j<SUBJECTS;j++)
                {
                printf("%d\n",marks[i][j]);
                }
            }
        
        
        return 0;
        }
    inputmarks(char *string[STUDENTS],int arr[][SUBJECTS+1])
        {
        int i,j,(*mrks)[SUBJECTS+1]=arr;
        
        for(i=0;i<STUDENTS;i++)
            {
            string[i]=(char *)malloc(20*sizeof(char));
            printf("enter name");
            scanf("%s",string[i]);
            printf("\nEnter mrks of %d subjects",SUBJECTS);
            for(j=0;j<SUBJECTS;j++)
                {
                scanf("%d",&(*(mrks+i))[j]);
                }
            }
    //shows that the names are getting stored in the string array
        for(i=0;i<STUDENTS;i++)
            {
            printf("------------------------------\n%s\n-----------------------------",string[i]);
            }
            return 0;
        }
    -----------------------------------------------------------------------------------------------------------------
    1_5.c
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    void edit(char *,int);
    int main()
        {
        char *stri;
        int k;
        stri=(char *)malloc(sizeof(char));
        printf("enter string");
        scanf("%[^\n]",stri);
        k=strlen(stri);
        edit(stri,k);
        printf("%s\n",stri);
        return 0;
        }
    void edit(char *str,int len)
        {
        int newlen=0,count=0;
        int i;
        while(*str)
            {
            if(*str++ ==' ') 
            {
            count++;
            }
        
            }
        printf("%d",count);
        newlen=len+2*count;
        printf("%d",newlen);
        str[newlen]='\0';
        for(i=len-1;i>=0;i--)
            {
            //printf("%d",i);
            if(str[i]==' ')
                {
                //printf("=--------------");
                str[newlen-1]='0';
                str[newlen-2]='2';
                str[newlen-3]='%';
                newlen=newlen-3;
                }
            else
                {
                
                str[newlen-1]=str[i];
                //printf("%s\n",str);
                newlen=newlen-1;
                
                }
            }
            //printf("----------%s",str);
                
        }


  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    first program :
    (13): warning C4013: 'inputmarks' undefined; assuming extern returning int

    it can be fixed by moving the code for inputmarks above the main. then it compiles with worse warnings.
    (35): warning C4047: 'function' : 'char **' differs in levels of indirection from 'char [2][20]'
    (35): warning C4024: 'inputmarks' : different types for formal and actual parameter 1

    don't bother even trying to debug when your program compiles with warnings. fix them first.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    Code:
        char name[STUDENTS][20]={'\0'};
        int marks[STUDENTS][SUBJECTS+1]={0};
        inputmarks(name,marks);
    ...
    inputmarks(char *string[STUDENTS],int arr[][SUBJECTS+1])
    {
    ...
            string[i]=(char *)malloc(20*sizeof(char));
    Once you fix the declaration, as suggested, you'll see that the first parameter doesn't match the declaration.

    But why are you allocating memory? The two arrays are declared on the stack of main(), and they should work just fine. The allocation wipes out the address of the first array on the stack. The entered data is saved to the allocated memory, which is lost when the function returns.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    Only 1 byte is being allocated in the second program. Try replacing
    Code:
        stri=(char *)malloc(sizeof(char));
    with
    Code:
        stri=(char *)malloc(512);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer doubt
    By Gil Carvalho in forum C Programming
    Replies: 37
    Last Post: 06-18-2012, 04:49 AM
  2. Doubt if this is bad pointer
    By gusth in forum C Programming
    Replies: 2
    Last Post: 09-08-2011, 12:24 AM
  3. pointer doubt....
    By roaan in forum C Programming
    Replies: 14
    Last Post: 07-29-2009, 11:20 AM
  4. Doubt in pointer.
    By shwetha_siddu in forum C Programming
    Replies: 5
    Last Post: 03-21-2009, 01:28 AM
  5. Doubt regarding pointer
    By karthik537 in forum C Programming
    Replies: 7
    Last Post: 01-21-2009, 09:53 AM