Yes "%s" was the culprit. Changed to "%c". 
Thank you all, I didn't expect this huge participation in this forum.

On Sunday, 16 March 2014 22:16:17 UTC+5:30, Dave Hylands wrote:
>
> Hi,
>
>
> On Sun, Mar 16, 2014 at 8:52 AM, milkyway <[email protected]<javascript:>
> > wrote:
>
>> Hi, I am new to this group. Following is a code I tried from Derek 
>> Malloy's videos, to read the gpio pin values. When I run this program I get 
>> "Segmentation fault". I can't make out how it happens. Can anybody help me 
>> out?
>>
>> #include <stdio.h>
>> #include <string.h>
>> #define MAX 128
>> int readButton(int);
>>
>> int main()
>> {
>>         int i = 1000;
>>         while(i--)
>>                 readButton(60);
>>         return 0;
>> }
>>
>>
>> int readButton(int pin)
>> {
>>         char setValue[4], GPIOPin[4], GPIODirection[MAX], GPIOValue[MAX];
>>         FILE *GPIOHandler;
>>         int i;
>>         sprintf(GPIOPin, "%d", pin);
>>
>
> This isn't your problem, but I always recommend to use bounded functions. 
> So use:
>
> snprintf(GPIOPin, sizeof(GPIOPin), "%d", pin);
>  
>
>>         sprintf(GPIODirection, "/sys/class/gpio/gpio%s/direction", 
>> GPIOPin);
>>         sprintf(GPIOValue, "/sys/class/gpio/gpio%s/value", GPIOPin);
>>         if ((GPIOHandler = fopen("/sys/class/gpio/export", "ab")) == NULL
>> )
>>         {
>>                 fprintf(stderr, "Failed to export the GPIO\n");
>>                 return 1;
>>         }
>>         strcpy(setValue, GPIOPin);
>>
>
> I also dislike strcpy (because its unbounded). strlcpy is better (if its 
> available). If strlcpy isn't available, I write my own version.
>  
>
>>         fwrite(GPIOPin, sizeof(char), 2, GPIOHandler);
>>
>
> You're arbitrarily writing 2 chars here. What about when pin is "999" or 
> "3" ?
> You probably want strlen(GPIOPin) instead.
>  
>
>>         fclose(GPIOHandler);
>>         if ((GPIOHandler = fopen(GPIODirection, "rb+")) == NULL)
>>         {
>>                 fprintf(stderr, "Failed to set the direction\n");
>>                 return 1;
>>         }
>>         strcpy(setValue, "in");
>>         fwrite(&setValue, sizeof(char), 3, GPIOHandler);
>>
>
> Using a length of 3 writes the trailing null. I'm not sure if that was 
> intentional or not.
>  
>
>>         fclose(GPIOHandler);
>>
>>         if ((GPIOHandler = fopen(GPIOValue, "rb+")) == NULL)
>>         {
>>                 fprintf(stderr, "Failed to set read file\n");
>>                 return 1;
>>         }
>>         fread(&setValue, sizeof(char), 1, GPIOHandler);
>>
>
> setValue is an array, so I tend to either use setValue or &setValue[0]. 
> Both of those constructs work properly with arrays or pointers. &setValue 
> doesn't work if setValue is a pointer.
>  
>
>>         fclose(GPIOHandler);
>>         printf("The value of the pin is %s\n", setValue[0]);
>>
>
> I think that this is your segmentation faule. setValue[0] is a character. 
> %s wants a character pointer. Either use setValue or &setValue[0]
>  
>
>>         if ((GPIOHandler = fopen("/sys/class/gpio/unexport", "ab")) ==NULL
>> )
>>         {
>>                 fprintf(stderr, "Failed to unxport GPIO \n");
>>                 return 1;
>>         }
>>         strcpy(setValue, GPIOPin);
>>         fwrite(&setValue, sizeof(char), 2, GPIOHandler);
>>         fclose(GPIOHandler);
>>         return 0;
>> }
>>
>>
>>
> -- 
> Dave Hylands
> Shuswap, BC, Canada
> http://www.davehylands.com 
>

-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to