0

I had a code which works fine with the integers

#include<stdio.h>

int main()
{
    int i;
    int* p[5];  

    printf("Enter the elements to be shorted");

    for(i=0;i<=4;i++)
    {
        scanf("%d\n",&p[i]);
    }
    for(i=0;i<=4;i++)
    {
        printf("entered  [%d] integers are = %s",i, p[i]);
    }
    return 0;
}

produce a output

Enter the strings to be shorted1
2
3
4
5
6
enetered  [0] string is = 1 
enetered  [1] string is = 2 
enetered  [2] string is = 3 
enetered  [3] string is = 4 
enetered  [4] string is = 5 

but when i change limne int* p[5] to char* p[5] for using it as array of pointers to string and do the necessary changes in the above code, it produces segmentation fault.I read ian a book that we cant do this as some garbage value will be assigned to the array of pointers to string.So what can be the possible way to implement the above code with array of pointers to string.

what i want to do is get the strings as input from users and store them in array of pointers to string and then get them printed at initial stage.I am trying to code for simplest string shorting.

5
  • 1
    The "string version" fails but you post the "integer version" ? Please show us the code which produces the error. Commented Jun 19, 2013 at 14:59
  • 1
    Is there a reason why you're using int *p[5] instead of int p[5] ? Commented Jun 19, 2013 at 15:04
  • yes p[5] m ay have also done the same work, but at present i am just trying to use pointers..in order to understand them more Commented Jun 19, 2013 at 15:22
  • @shailendra No offense, but you're using them wrong. You shouldn't use pointers in place of integers. While its unlikely its possible that the size of a pointer be less than the size of an integer. On platforms where this is true, your program wouldn't work. Commented Jun 19, 2013 at 15:29
  • got it..this program works equally fine with simple array of integers. Commented Jun 19, 2013 at 15:31

3 Answers 3

2

Make sure you reserve room for the characters of the strings:

char p[5][128];

and also make sure you limit the length when reading, so scanf() doesn't write outside the buffer:

if(scanf("%127s", p[i]) == 1)
{
  p[i][127] = '\0'; /* Make sure it's terminated. */
}
Sign up to request clarification or add additional context in comments.

3 Comments

I think you meant to remove the * from your declaration as well.
@unwind: i am getting accurate result but in scanf its taking 6 input instead of asking for 5 , but output is always coming correct
this is what coming root@ubuntu:~/C/arrays/shorting# ./stringshorting Enter the strings to be shortedshailendra ravi jas mayank vivek ankur enetered [0] string is = shailendra enetered [1] string is = ravi enetered [2] string is = jas enetered [3] string is = mayank enetered [4] string is = vivek
0

First of all, the int array definition is wrong in your first test. it should be

int p[5]; 

Second you have to allocate memory for each pointer in your array. each element in your array char *p[5]; is a pointer.

You can use directly by scanf

char *p[5];
for(i=0;i<=4;i++)
{
scanf("%ms\n",&p[i]);
}

But this is not portable is available only for gcc>gcc2.7

or you have to allocate memory each time before scanf()

char *p[5];
for(i=0;i<=4;i++)
{
p[i]=calloc(100,1);
scanf("%99s\n",p[i]);
}

3 Comments

I am just declaring array of 5 pointers to integer by using int* p[5];and then using them while printing, this works fine for me in case of array.
@shailendra But its wrong. int *p[5] create an array of 5 pointer to int. Not an array of 5 int. So basicaly, when you do scanf("%d\n", &p[1]); you're storing an integer where a pointer to integer should be stored. It works okay because sizeof(int *) >= sizeof(int) but its wrong.
i think i used, int* p[5] instead of int *p[5] and if both are same than i think i have to make my base more strong..what i read in link is that they both are same.
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
    int i;
    char *p[5];  

    printf("Enter the string\n");

    for(i=0;i<=4;i++){
        char buff[128];
        scanf("%127s", buff);
        p[i] = strdup(buff);
    }
    for(i=0;i<=4;i++){
        printf("entered  [%d] string is = %s\n", i+1, p[i]);
    }
    for(i=0;i<5;++i) free(p[i]);
    return 0;
}

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.